2

I'm having trouble converting a curl that works to an equivalent http request through nodeJS. I'm using the Request module, but I seem to be doing something wrong when making the request. When I run it, it gives me

body: Cannot POST /path

Not really sure how to debug this, any ideas?

var data = JSON.stringify({
  'sender': {
    'name': 'name',
    'handle': 'handle'
  }, 
  'subject': 'Title here',
  'body': 'something something',
  'metadata': {}
});

var options = {
    host: 'website.com',
    path: '/path',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer <token>',
        'Accept': 'application/json',
        'Content-Length': Buffer.byteLength(data)
    }
};

var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log("body: " + chunk);
    });
});
req.write(data);
req.end();

Below is the equivalent curl (that works) that I'm trying to make for the above nodejs.

curl --include \
    --request POST \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer <token>" \
    --header "Accept: application/json" \
    --data-binary "{
    \"sender\": {
       \"name\": \"name\",
       \"handle\": \"handle\"
    },
    \"subject\": \"Title here\",
    \"body\": \"something something\",
    \"metadata\": {}
    }" \
    'website.com/path"

1 Answer 1

2

You can include your JSON data directly with json parameter with request library :

var request = require('request');

var options = {
    uri: 'http://website.com/path',
    method: 'POST',
    headers: {
        'Authorization': 'Bearer <token>',
        'Accept': 'application/json'
    },
    json: {
        'sender': {
            'name': 'name',
            'handle': 'handle'
        },
        'subject': 'Title here',
        'body': 'something something',
        'metadata': {}
    }
};

var req = request(options, function(error, response, body) {

    if (error) {
        console.log(error);
        return;
    }
    if (response.statusCode == 200) {
        console.log(body);
    } else {
        console.log("receive status code : " + response.statusCode);
    }
});

From request options doc :

json - sets body to JSON representation of value and adds Content-type: application/json header. Additionally, parses the response body as JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you! I had trouble understanding the docs and looked for examples, but this seemed to clarify it. I thought data had to be sent after defining the request
Glad it helped, you can accept the answer if it's good for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.