0

I am using NodeJS to send a PUT request to a private API that I have access to. When using curl the API sends back JSON and everything works great, but when using NodeJS it isn't sending back anything. I am very new to this and I know my code isn't the best, but it works (other than this one problem). I am wondering how to display the JSON that I should be retrieving back from the the PUT request in the terminal.

NodeJS Server Code:

app.post('/myaction', function(req, res) {
    var data = req.body.service;
    var data1 = req.body.serviceId;
    console.log(data);
    console.log(data1);
    res.sendFile(__dirname + '/services.html');
    myURL = 'http://privateAPI.com/service_instance/' + data1;
    console.log(myURL)
request({
    method: 'PUT',
    uri: myURL,
    multipart: [{
       'content-type':'application/json',
        body: data 
    }]
    }, function(error, request, body){
    })
});

When I console.log(body) I get the HTML of the API. When I console.log(request) I get the JSON I am sending to the API. When I console.log(error) I get Null.

Links I've read but didn't help: How to retrieve POST query parameters?
How to retrieve POST query parameters?

Community
  • 1
  • 1
  • The callback you provide to `request` receives `error`, and `body`, both of which may be helpful in determining what is going on – Nick Tomlin Aug 31 '16 at 17:16
  • I've `console.logg`ed all of them I will edit question and post their responses –  Aug 31 '16 at 17:18

1 Answers1

0

Actually you don't need to use multipart.

But the problem is that you need to set the content type you expected from the API.

request({
    method: 'PUT',
    uri: myURL,
    headers: {
      'accept': 'application/json',
      'content-type': 'application/json'
    },
    multipart: [{
       'content-type':'application/json',
        body: data 
    }]
    }, function(error, response, body){
       console.log(response);
    })

Without multipart

request({
    method: 'PUT',
    uri: myURL,
    headers: {
      'accept': 'application/json',
      'content-type': 'application/json'
    },
    body: data,
    json: true
    }, function(error, response, body){
       console.log(response);
    })
Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
  • Thank you so much for your response! I have just tested this code and I have received no errors, but nothing comes up in the terminal? Is there anyway to `console.log` it so I can see the header? –  Aug 31 '16 at 17:50
  • @CtrlAltDelete I updated my answer. You have `response` and can see headers, etc. – Ebrahim Pasbani Aug 31 '16 at 17:52
  • @CtrlAltDelete You said "nothing comes up". You mean `body` has nothing? – Ebrahim Pasbani Aug 31 '16 at 17:53
  • When I said nothing comes up, I was not `console.log(body)`. I wasn't console.logging anything in fact. I did the `console.log(response)` and it returns JSON but not the JSON that I want to be returned. When using curl it returns a specific JSON back, and the JSON that the `res` is sending back does not contain it. Thanks again! Edit: It also returns the HTML of the API in a very unstructured form –  Aug 31 '16 at 17:59
  • @CtrlAltDelete There should be in `response.body` or `body`. This is the routine. If this doesn't work, please remove multipart – Ebrahim Pasbani Aug 31 '16 at 18:13
  • What should I put in place of multipart? –  Aug 31 '16 at 18:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122335/discussion-between-ctrlaltdelete-and-ebrahim-pasbani). –  Aug 31 '16 at 18:31