NodeJS and HTTP Client - Are cookies supported?

前端 未结 6 829
孤街浪徒
孤街浪徒 2021-01-03 20:34

NodeJS is a fantastic tool and blazing fast.

I\'m wondering if HTTPClient supports cookies and if can be used in order to simulate

6条回答
  •  粉色の甜心
    2021-01-03 21:12

    Short answer: no. And it's not so great.

    I implemented this as part of npm so that I could download tarballs from github. Here's the code that does that: https://github.com/isaacs/npm/blob/master/lib/utils/fetch.js#L96-100

    var cookie = get(response.headers, "Set-Cookie")
    if (cookie) {
      cookie = (cookie + "").split(";").shift()
      set(opts.headers, "Cookie", cookie)
    }
    

    The file's got a lot of npm-specific stuff (log, set, etc.) but it should show you the general idea. Basically, I'm collecting the cookies so that I can send them back on the next request when I get redirected.

    I've talked with Mikeal Rogers about adding this kind of functionality to his "request" util, complete with supporting a filesystem-backed cookiejar, but it's really pretty tricky. You have to keep track of which domains to send the cookies to, and so on.

    This will likely never be included in node directly, for that reason. But watch for developments in userspace.

    EDIT: This is now supported by default in Request.

提交回复
热议问题