Alamofire HTTP request to local web server

I am developing an application for the iPhone, and the plan is to send the JSON package so often from the application to the local web server. For this, I planned to use Alamofire . My POST method is as follows:

Alamofire.request(Alamofire.Method.POST, "http://XXX.XX.X.XX:3000/update", parameters: dictPoints, encoding: .JSON) .responseJSON {(request, response, JSON, error) in println(JSON) } 

The IP address is allocated, but I made sure that it matches the IPv4 IP address of my local server. The server is configured to listen on port 3000. The server configuration looks like this:

 var express = require('express'); var app = express(); var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function (callback) { console.log("MongoDB connection is open."); }); // Mongoose Schema definition var Schema = mongoose.Schema; var LocationSchema = new Schema({ //some schema here }); // Mongoose Model definition var LocationsCollection = mongoose.model('locations', LocationSchema); // URL management app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); }); app.get('/update', function (req, res) { console.log("Got something from the phone!"); }); // Start the server var server = app.listen(3000, function () { var host = server.address().address var port = server.address().port console.log('App listening at %s:%s',host, port) }) 

So this server is working fine. I can check it in my browser and type in the URL: http: //127.0.0.1haps000 , and it will feed me the index.html file. If I type in http: //127.0.0.1haps000/update ... then I get the message "Got something from the phone!". message. However, when I launch my application (make sure that my phone is on the same wireless network as the server) and the Alamofire method is called ... the answer I get is zero. I also don’t see "Got something from the phone!" message. Can someone tell me why this will happen ... or better yet, how to fix it?

+5
source share
3 answers

A few thoughts:

  • You create a POST request in Alamofire, but you told Express to handle GET requests for /update . Use app.post(...) if you want to treat it as POST .

  • Your Alamofire code is looking for a JSON response, but you don't seem to be creating a JSON response. In the short term, you can use responseString rather than responseJSON , but I assume that you really want to change your web service to respond with JSON (to make it easier for the application to parse the responses).

  • Your Alamofire code sends a JSON request (but clearly, when you send a request through a web browser, it is not JSON). Are you sure you want to send a JSON request? (This should not be confused with the JSON response problem.) Would you like to use a parameter of type .URL of the encoding type, not .JSON ?

  • Whenever you have a query that works correctly from a web browser, but not from an application, it is useful to look like with a tool like Charles , and then you can compare how they differ and diagnose the source of the different behavior.

+5
source

If you use the Express server and iOS Simulator on the same computer, try using http://0.0.0.0:<port>/<url> instead of the actual IP, this helped in my case.

+2
source

It looks like the node server is not bound to the address you expect. You have to make sure that when you enter the actual IP address in your http: //XXX.XX.X.XX: 3000 / update in your web browser to which it responds. Your question data indicates that you just used the loopback address.

0
source

Source: https://habr.com/ru/post/1212744/


All Articles