Saturday 1 April 2017

Neither Node.js PUT or POST routes are presenting the data that is being received

I tried this with a number of different modules and methods. Even by proving the other modules do work as expected in Node by building a separate test project and testing each module individually.

Posting FROM Node's hosted router to a remote API (not TO Node's hosted API)

This problem is not one of SENDING data to an API. It must IMO a problem in the receiving API's not giving up the data it IS receiving for some reason.

I've proven the PUT or POST calls are sending the data by sending the call to http://httpbin/org. That site shows me I'm sending what I expect to be sending.

Here is how I'm sending. I can even see in the receiving API that that API is certainly getting called successfully.

-- sending -- ((Again. This shows my node.http attempt. But I get the same problem using requestjs, requestifyjs, needlejs))

router.get('/', function (req, res, next) {

    var hst = req.headers.host.split(':');
    var lookbackURL = 'https://' + req.headers.host + req.baseUrl;
    lookbackURL = 'http"httpbin.org/put';
    var dat = {
        what: 'ever'
        , try: 'again'
    };
    var bdy = JSON.stringify(dat);
    var options = {
        host: hst[0], port: hst[1], path: req.baseUrl, method: 'PUT'
        , headers: { 'Content-Type': 'application/json' }
    };

    var r = nodeHttp.request(options); r.write(bdy); r.end();
    res.sendStatus(200);

});

-- receiving --

router.put('/', function (req, res, next) {
    console.log('r', req);
});

No matter what module or method I use, in all cases, the receiving req object doesn't contain the what or try data.

BUT in my test project the data is there as I expect it to be, in all cases. Doing the same console.log(req); in the test project, reqestjs, requestjs, needlejs, node.http all show a proper body object. But in this problem there isn't a body object in req. And sending this put/post to http://httpbin.org I can see the body object is being sent.

Any ideas?



via user2367083

No comments:

Post a Comment