0

i can't figure out what to place in exchange of the JSON.stringify syntax in the body parameter. It is returning a SyntaxError with a code of 800A03EA

const request = require('request');

const username = 'myUserName';
const password = 'myPassword';
const options = {
    method: 'POST',
    url: 'https://siteToPostTo.com/api/v1/statuses',
    auth: {
        user: username,
        password: password
    },
    body: JSON.stringify({
        status: 'automated message to post'
    })
};

request(options, function(err, res, body) {
    if (err) {
        console.dir(err);
        return;
    }
    console.log('headers', res.headers);
    console.log('status code', res.statusCode);
    console.log(body);
});

1 Answer 1

2

Nothing. Instead, add

json: true to your options and don't attempt any stringification. request() will do the magic for you.

const request = require('request');

const username = 'myUserName';
const password = 'myPassword';
const options = {
    method: 'POST',
    url: 'https://siteToPostTo.com/api/v1/statuses',
    auth: {
        user: username,
        password: password
    },
    json: true,
    body: {
        status: 'automated message to post'
    }
};

request(options, function(err, res, body) {
    if (err) {
        console.dir(err);
        return;
    }
    console.log('headers', res.headers);
    console.log('status code', res.statusCode);
    console.log(body);
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.