Getting and Setting Cookies in drupal_http_request

Posted in |

A Drupal 6 http client request is constructed so:

$http_results = drupal_http_request($url, array(), 'GET', NULL, 3);

Some URLs, however, require that a cookie is passed to each request to ensure that a correct response is issued. First, however, the cookie must be retrieved from an initial request.

$http_results = drupal_http_request($url, array(), 'GET', NULL, 3);
$cookie = $http_results->headers['Set-Cookie'];
$headers = array('Cookie' => $cookie);

Subsequent requests can then include the cookie by specifying the header.

$http_results = drupal_http_request($url, $headers, 'GET', NULL, 3);
Submitted by Jonathan Mitchell on Sat, 05/30/2009 - 15:13

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vega (not verified) Says:
Mon, 09/28/2009 - 12:37

Hi, i'm trying to extract an xml from an api in which i have to login before get the xml file.
The url for login is like this: http://localhost:8080/path?username=username&password=pass
After that, the api redirects to http://localhost:8080/anotherpath where is the xml

So i tried to get the drupal_http_request(login_url) and then send the cookie in other drupal_http_request(redirect_url, header[cookie]) that i get but it doesn't work.

What i'm doing wrong?

Jonathan Mitchell Says:
Tue, 09/29/2009 - 12:16

You need to to analyse the data exchange to ensure that you are formatting your request correctly. Sometimes it takes perseverance to ensure that you have constructed your request correctly.

Use a protocol analyser to see what is going on. I use wireshark.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
Let us know you are human.
16 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.