If you want to access the raw HTTP message, I would suggest using net module instead and writing the request yourself. Something like this for a simple GET request:
var net = require('net'); var host = 'stackoverflow.com', port = 80, socket = net.connect(port, host, function() { var request = "GET / HTTP/1.1\r\nHost: " + host + "\r\n\r\n", rawResponse = "";
For a POST request sending application/x-www-form-urlencoded data, you can write a request using something like:
function writePOSTRequest (data, host, path) { return "POST " + path + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + Buffer.byteLength(data) + "\r\n\r\n" + data + "\r\n\r\n"; } var data = "name1=value1&name2=value2", request = writePOSTRequest(data, host, "/path/to/resource");
where I use Buffer.byteLength because Content-Length requires a length in bytes rather than characters. Also, remember that data must be URL encoded.
If you know little about the format of HTTP messages, then this is a decent place to run:
http://jmarshall.com/easy/http/
In addition, if you do not know what the response encoding will be, you will have to parse the headers first, but UTF -8 is by far the most common , so this is a fairly safe bet.
source share