Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js POST request

Tags:

http

node.js

I looked at the api but I couldn't find it.

Where/How should I put data on a POST request on client.request() or client.request("POST" ,...)?

like image 248
mdikici Avatar asked Jul 07 '10 07:07

mdikici


People also ask

How do you POST a request in node JS?

Example code: var request = require('request') var options = { method: 'post', body: postData, // Javascript object json: true, // Use,If you are sending JSON data url: url, headers: { // Specify headers, If any } } request(options, function (err, res, body) { if (err) { console. log('Error :', err) return } console.

How do I accept a POST request in node?

To respond to POST requests, simply type response. send(“Whatever string or other variable you want to send goes here“); or response. write(“Thank you” or variable containing HTML file); inside of the app. post function to let the user know if their submission has been received.

What is POST request in Express js?

js POST Method. Post method facilitates you to send large amount of data because data is send in the body. Post method is secure because data is not visible in URL bar but it is not used as popularly as GET method. On the other hand GET method is more efficient and used more than POST.

What is in a POST request?

In computing, POST is a request method supported by HTTP used by the World Wide Web. By design, the POST request method requests that a web server accept the data enclosed in the body of the request message, most likely for storing it. It is often used when uploading a file or when submitting a completed web form.


2 Answers

Maybe you should look closer then.

This is straight from the node.js API documentation:

request_headers is optional. Additional request headers might be added internally by Node. Returns a ClientRequest object.

Do remember to include the Content-Length header if you plan on sending a body. If you plan on streaming the body, perhaps set Transfer-Encoding: chunked.

NOTE: the request is not complete. This method only sends the header of the request. One needs to call request.end() to finalize the request and retrieve the response. (This sounds convoluted but it provides a chance for the user to stream a body to the server with request.write().)

request.write() is for sending data.

So you do it like this (more or less):

var rq = client.request('POST', 'http://example.org/', {'Content-Length': '1024'});
var body = getMe1024BytesOfData();

rq.write(body);
rq.end();

This code is just here to get the concept across. I have NOT tested it in any way.

like image 118
selfawaresoup Avatar answered Oct 03 '22 07:10

selfawaresoup


For more easier client requests you can use request module. It takes care of all the hard work and has a simple API.

like image 36
Farid Nouri Neshat Avatar answered Oct 03 '22 08:10

Farid Nouri Neshat