davetemplin / web-request

Simplifies making web requests with TypeScript async/await
MIT License
35 stars 8 forks source link

how to use WebRequest to post JSON data #16

Open martin-sun opened 7 years ago

martin-sun commented 7 years ago

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?

martin-sun commented 7 years ago

I just check the README.md and notice the command should add typings/node/node.d.ts tsc index.ts typings/node/node.d.ts --target es6 --module commonjs

when I run above command, I will get below error. error TS6053: File 'typings/node/node.d.ts' not found.