0

I'm trying to replicate a very straightforward 'POST' API signout call with my Express app running on localhost. It should take the signout info and pass it from localhost to MY-SERVER

How it should look:

 curl "http://MY-SERVER/api/2.2/auth/signout" -X POST -H "Auth:1245"

I'm using request.post, set in a separate route file "signout.js" (see below).

var express = require('express');
var request = require('request');
var cheerio = require('cheerio');
var router = express.Router();
var OnlineApiUrl = 'https://MY-SERVER/api/2.2/';

//Sign Out
router.post('/auth/signout', function(req, res) {
 var authentication_token = req.query.auth_token;

request.post({
url: OnlineApiUrl + 'auth/signout',
headers: {
      'Auth': authentication_token
}
}, function(err, response, body) {
var $ = cheerio.load(body);
var hasError = $('error').length;
if (hasError) {
  response = {
    http_code: $('error').attr('code'),
    http_message: $('summary').text()
  };
} else {
  response = {
    http_code: 204
  };
}
return res.json(response);
});
});
module.exports = router;

From what I can tell it should work, yet when I run

curl https://localhost:8080/api/auth/signout?auth=12345  --cacert /etc/nginx/ssl/server.crt 

It only returns "Cannot GET /api/auth/signout?auth=12345"

Any idea what I'm missing here? It only needs to pass that custom header, nothing else. I'm also not sure why it's giving me "Cannot GET" for a POST request either...

KDC
  • 1,441
  • 5
  • 19
  • 36
  • How does your server handle the POST request? Also worth reading http://stackoverflow.com/questions/611906/http-post-with-url-query-parameters-good-idea-or-not – br3w5 May 18 '16 at 12:43
  • The server uses bodyParser: app.use(bodyParser.urlencoded({ extended: true })); Frankly I'm thinking it might need 'Content-type' or 'Content-length: 0' or something like that in the header? Could that be it? – KDC May 18 '16 at 12:50

1 Answers1

0

I figured out the solution. The problem was that I was using 'POST' two times: both in the higher-level 'router.post' and 'request.post'. Putting the higher-level one as 'router.get' (while keeping 'request.post') solved it.

KDC
  • 1,441
  • 5
  • 19
  • 36