Skip to content

how to use WebRequest to post JSON data #16

@martin-sun

Description

@martin-sun

import * as WebRequest from 'web-request';

(async function () {
var data = {
"id": 5,
"title": "json-server5",
"author": "typicode5"
};
await WebRequest.post('http://192.168.22.10:3000/posts', {headers: {'Content-Type': 'application/json'}}, data);
})();

I try to use WebRequest post JSON data with above code.
but always get fatal error:

(node:45899) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Argument error, options.body.
_http_outgoing.js:456
throw new TypeError('First argument must be a string or Buffer');
^

TypeError: First argument must be a string or Buffer
at ClientRequest.OutgoingMessage.write (_http_outgoing.js:456:11)
at Request.write (/Users/martinsun/node_modules/request/request.js:1514:27)
at end (/Users/martinsun/node_modules/request/request.js:552:18)
at Immediate. (/Users/martinsun/node_modules/request/request.js:581:7)
at runCallback (timers.js:637:20)
at tryOnImmediate (timers.js:610:5)
at processImmediate [as _immediateCallback] (timers.js:582:5)

Would you please let me know how to fix the problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions