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" ,...)
?
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.
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.
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.
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.
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.
For more easier client requests you can use request module. It takes care of all the hard work and has a simple API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With