How can I make an outbound HTTP POST request, with data, in node.js?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- HTML form is not sending $_POST values
- Google Apps Script: testing doPost() with cURL
- How to instantiate Http service in main.ts manuall
Here's an example of using node.js to make a POST request to the Google Compiler API:
I've updated the code to show how to post data from a file, instead of the hardcoded string. It uses the async
fs.readFile
command to achieve this, posting the actual code after a successful read. If there's an error, it is thrown, and if there's no data the process exits with a negative value to indicate failure.Simple and dependency-free. Uses a Promise so that you can await the result. It returns the response body and does not check the response status code.
Usage:
After struggling a lot while creating a low level utility to handle the post and get requests for my project, I decided to post my effort here. Much on the lines of accepted answer, here is a snippet for making http and https POST requests for sending JSON data.
This gets a lot easier if you use the request library.
Aside from providing a nice syntax it makes json requests easy, handles oauth signing (for twitter, etc.), can do multi-part forms (e.g. for uploading files) and streaming.
To install request use command
npm install request
By using request dependency.
Simple solution :