Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a HTTP Client Request with a cookie?

I've got a node.js Connect server that checks the request's cookies. To test it within node, I need a way to write a client request and attach a cookie to it. I understand that HTTP Requests have the 'cookie' header for this, but I'm not sure how to set it and send -- I also need to send POST data in the same request, so I'm currently using danwrong's restler module, but it doesn't seem to let me add that header.

Any suggestions on how I can make a request to the server with both a hard-coded cookie and POST data?

like image 803
Vanwaril Avatar asked Jan 02 '11 18:01

Vanwaril


People also ask

How do I set-cookie in HTTP request?

The Set-Cookie header is sent by the server in response to an HTTP request, which is used to create a cookie on the user's system. The Cookie header is included by the client application with an HTTP request sent to a server, if there is a cookie that has a matching domain and path.

Does HTTP request contain cookies?

The Cookie HTTP request header contains stored HTTP cookies associated with the server (i.e. previously sent by the server with the Set-Cookie header or set in Javascript using Document. cookie ). The Cookie header is optional and may be omitted if, for example, the browser's privacy settings block cookies.

How do I set-cookie in HTTP header?

The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.

Can the client set a cookie?

Cookies can also be manipulated by the client. To set a cookie, you can assign a value to document. cookie in the format of key=value . If the key already exists, it will be overwritten.


1 Answers

This answer is deprecated, please see @ankitjaininfo's answer below for a more modern solution


Here's how I think you make a POST request with data and a cookie using just the node http library. This example is posting JSON, set your content-type and content-length accordingly if you post different data.

// NB:- node's http client API has changed since this was written // this code is for 0.4.x // for 0.6.5+ see http://nodejs.org/docs/v0.6.5/api/http.html#http.request  var http = require('http');  var data = JSON.stringify({ 'important': 'data' }); var cookie = 'something=anything'  var client = http.createClient(80, 'www.example.com');  var headers = {     'Host': 'www.example.com',     'Cookie': cookie,     'Content-Type': 'application/json',     'Content-Length': Buffer.byteLength(data,'utf8') };  var request = client.request('POST', '/', headers);  // listening to the response is optional, I suppose request.on('response', function(response) {   response.on('data', function(chunk) {     // do what you do   });   response.on('end', function() {     // do what you do   }); }); // you'd also want to listen for errors in production  request.write(data);  request.end(); 

What you send in the Cookie value should really depend on what you received from the server. Wikipedia's write-up of this stuff is pretty good: http://en.wikipedia.org/wiki/HTTP_cookie#Cookie_attributes

like image 115
RandomEtc Avatar answered Sep 26 '22 02:09

RandomEtc