Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to use get($url) to retrieve the contents of a web page, but I need to have a cookie set in order for the request to work. Is there a function similar to get() which allows a cookie to be set in the header of the URL before the call?

Thank you!

  • Comment on How can I send a cookie as part of an HTTP request?

Replies are listed 'Best First'.
Re: How can I send a cookie as part of an HTTP request?
by chipmunk (Parson) on Dec 14, 2000 at 23:59 UTC
    The LWP package includes modules for handling cookies, but they're not documented in LWP or lwpcook. Instead, you have to look at HTTP::Cookies and LWP::UserAgent.
Re: How can I send a cookie as part of an HTTP request?
by kilinrax (Deacon) on Dec 14, 2000 at 23:35 UTC
    Assuming you mean the header of a HTTP request, have a look at HTTP::Header
Re: How can I send a cookie as part of an HTTP request?
by cei (Monk) on Dec 25, 2000 at 05:26 UTC
    Keep in mind that you can only set cookies for your own domain. If your get($url) is pulling from someone else's machine, and you're wanting to pre-login to that machine by making a cookie for it, it won't work. On the other hand, if you want to use the cookie to keep track of where someone is on your site before pulling contents from another site, that should work fine.

    I use for my cookie handling.

      Actually, you can specify a cookie that gets sent to another domain. That's what the 'domain' parameter in CGI's cookie method is for. (If it's not specified, the default domain is the originating domain.)

      Note, however, that browsers often have three settings for cookies; all cookies accepted, all cookies rejected, and cookies accepted except for cookies that aren't returned to the originating domain.

      All that is irrelevant to the current question, of course; using LWP's get() method, you can send whatever cookies you want with the request.

        Uhh, no that is exactly wrong. Clients are expected to ignore cookies that don't come from matching domains. If you request a resource from then the server that responds can set cookies for * or but nothing else. What you are mixing in is the protection against you going to "" and that page calling a resource from "" and you picking up a cookie from a site you didn't even know you visited.

        All the domain parameter is for is setting the depth into your own domain. That way a server at can set one cookie for all * and another at just * and one just for itself at! If I could set cookies in the domain for you from you can be sure I would be... =)

        LWP is most certainly the right answer tho! Look at HTTP::Request's perldocs specifically

        $you = new YOU;
        honk() if $you->love(perl)

Re: How can I send a cookie as part of an HTTP request?
by vg2903 (Initiate) on Mar 01, 2020 at 06:56 UTC
    i do have 102 rss in my blog and these help me alot for the upcoming things what i love.