0

I have a middle-layer that works as a copuling between a frontend and an api-service. For all posts, I simply want to forward them to the api-service.

For example when I post this form:

<form method="post" action="../rest/1/comment/create" class="questionResponseForm expandable">
        <textarea name="content" class="questionResponseTextarea"></textarea>
        <input type="hidden" name="code" value="1-1454406440-58e7fa2e7897ffb90c9391febdd9c49c2bd2f3d6">
        <input type="hidden" name="q_id" value="1425">
        <input type="hidden" name="p_id" value="1425">
        <button type="submit" class="questionResponseButton trigger"><svg class="questionTitleIcon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-pencil"></use></svg>Kommentera frågan</button>
</form>

I want to post the exact same form but to another server.

I use requstify for this:

app.apiUrl in my case is localhost (the node server runs on localhost:3000) req.originalUrl makes sure the same url is used. req.body contains the post-parameters.

app.post('/rest/1/:object/:method', post);

function post(req,res){
    var fullUrl = app.apiUrl + req.originalUrl,
        requestify = require('requestify');

    requestify.post(fullUrl, req.body).then(function(response) {
        console.log(response);
        app.res.render('master',response);
    })
}

req.body:

{ content: 'öljkökljölkjölkj',
  code: '1-1454409967-dd95a558b2753d8f2f6239c1a2614b32d51474d0',
  q_id: '1422',
  p_id: '1422' }

"then" never seems to be triggered, making me suspect there is something wrong with the post.

Himmators
  • 14,278
  • 36
  • 132
  • 223
  • 1
    you are doing requestify.get, instead og requestify.post – null1941 Feb 02 '16 at 09:59
  • thanks! That wasn't it though, still can't log anything in "then". – Himmators Feb 02 '16 at 10:15
  • 1
    are you sure second param of requestify.post is dictionary and you are not sending somth null – null1941 Feb 02 '16 at 10:37
  • If you are refering to req.body, yes, that is what it's suppose to be. I checked it in the log.(updateing question) – Himmators Feb 02 '16 at 10:47
  • I'm okay with any other solution aswell, not bound by requestify. – Himmators Feb 02 '16 at 10:49
  • or some way to debug this better. – Himmators Feb 02 '16 at 10:49
  • 1
    for debugging you can use visual code IDE. :) – null1941 Feb 02 '16 at 10:51
  • try check http://unirest.io/nodejs.html – null1941 Feb 02 '16 at 10:52
  • 1
    If your fulfillment callback (then) is never called, perhaps an error was raised? You should attach a rejection handler as well, either as the second argument to then, or using fail(function(err) {}) chained to the existing then call (possibly depending on which promise flavor you're using *fail* could be called *catch*, or could be missing, in which case you should use the second argument to then). Since you are also not terminating the promise chain with done(), any errors would currently go unnoticed. – JHH Feb 02 '16 at 11:00
  • @JHH thanks! If you write an answer I'll give you the credit! – Himmators Feb 02 '16 at 11:37
  • requestify.post(fullUrl, req.body).then(function(response) { console.log(response); app.res.render('master',response); }).catch(function(err){ console.log('error:', err); }); – null1941 Feb 02 '16 at 12:01
  • @null1941 click the answer question button below! – Himmators Feb 02 '16 at 12:08
  • I posted my comment as an answer. Not sure why @null1941 posted the same thing. :( – JHH Feb 02 '16 at 12:14
  • $JJH, thanks to you I managed to figure out the cause of the actual problem: http://stackoverflow.com/questions/35153495/whats-the-difference-between-posting-from-a-form-and-posting-from-a-server – Himmators Feb 02 '16 at 12:26

2 Answers2

0

add catch block and debug what is happening in the post call. Modified requestify Post call

requestify.post(fullUrl, req.body).then(function(response) {    console.log(response); app.res.render('master',response); }).catch(function(err){ console.log('error:', err); });
null1941
  • 972
  • 8
  • 20
0

Since you mentioned that your promise never seems to be resolved (the callback registered by then is never called), I suspect that an error was raised, causing the promise to be rejected. From your code it's obvious that you are not registering any rejection handler, nor are you terminating the promise chain.

From the current info I am not able to determine what error might have been thrown and why, but in order to debug further you should add a rejection handler:

requestify.post(fullUrl, req.body).then(function(response) {
    console.log(response);
    app.res.render('master',response);
}).fail(function(err) {
    // add code here to handle error and/or debug reason for error
}).done();

Note that depending on the flavor of promises used, fail could be called catch, alternatively you could supply your rejection handler as the second argument to then:

requestify.post(fullUrl, req.body).then(function(response) {
    console.log(response);
    app.res.render('master',response);
}, function(err) {
    // add code here to handle error and/or debug reason for error
});

Also note that it's good practice to terminate promise chains using done, otherwise unhandled errors might go completely unnoticed, which might cause you to spend a lot of time not realizing where something went wrong:

somePromise.then(callback1).then(callback2).then(callback3).fail(errorhandler).done();

If an error is still unhandled when a promise chain is terminated, the error will be thrown on a future turn of the event loop, so it will at least be visible. Note that the availability of done may also depend on the promise flavor used.

JHH
  • 8,567
  • 8
  • 47
  • 91