简体   繁体   中英

How do I use request.js to send text as the request body?

I am attempting to use the request.js library for Node to send a post request with plain text as the body, but I'm finding that the server I'm sending to is receiving an empty body, or perhaps a body consisting of an empty object (logging the body yields {}).
If I send it using Postman, it works properly, so it seems clear that the problem is with my usage of request.js. The code of the relevant function (with the url changed) is as follows:

function (queryText) {
        const options = {
            url: "https://myurl.com/",
            body: queryText
        };
        return new Promise ((resolve, reject) => {
            request.post(options, (err, response) => {
                if (err) reject(err);
                else resolve(response);
            });
        });
    }

Anybody know what I'm doing wrong here?

Try adding content-type header like so:

const options = {
    url: "https://myurl.com/",
    body: queryText,
    headers: {
        'content-type': 'text/plain'
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM