Attempting to connect to a local server from Xcode. I imported Alamofire Pod into my Xcode project and executed the following command in xcode
Alamofire.request(.GET, "http://localhost:3000" , parameters: ["code": "123"]).responseJSON {
response in
print ("Hello", response)
}
I get the following error in Xcode when working on an iOS device.
FAILURE: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x13d84f7f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http:
I know that the local service is working. When I invoke the following command on the command line:
$ node index.js
Running at http:
The following is shown in the browser:
Cannot GET /
My .js file is as follows:
var buffer = require('buffer');
var bodyParser = require('body-parser');
var crypto = require('crypto');
var express = require('express');
var request = require('request');
var url = require('url');
var app = express();
var config = {
clientId: '',
clientSecret: '',
callbackUrl: '',
encryptionSecret: '',
endpoint: 'https://accounts.spotify.com',
};
var secretString = config.clientId + ':' + config.clientSecret;
var authHeader = 'Basic ' + new buffer.Buffer(secretString).toString('base64');
app.use(bodyParser.json());
var server = app.listen(3000, function() {
var address = server.address();
console.log('Running at http://localhost:%s', address.port);
});
app.post('/swap', function(req, res) {
console.log(req.body);
if (!req.body || !req.body.hasOwnProperty('code')) {
console.log('Swap: missing auth code');
res.status(550).send('Permission Denied');
return;
}
formData = {
grant_type: 'authorization_code',
redirect_uri: config.callbackUrl,
code: req.body.code
};
console.log('Swap: POST to %s', url.resolve(config.endpoint, '/api/token'), formData);
request.post({
url: url.resolve(config.endpoint, '/api/token'),
headers: {
'Authorization': authHeader,
'Content-Type': 'application/x-www-form-urlencoded',
},
form: formData,
}, function(error, response, body) {
if (error) {
console.log('Swap: Error - ', error);
res.status(500).send('Internal Server Error');
return;
}
if (res.statusCode != 200) {
debug('Swap: response: ', response.statusCode);
res.status(550).send('Permission Denied');
return;
}
var tokenData = JSON.parse(body);
console.log('Swap: tokenData - ', tokenData);
res.status(200).set({
'Content-Type': 'application/json',
}).send(tokenData);
});
});