NodeJS and HTTP Client - Are cookies supported?

前端 未结 6 811
孤街浪徒
孤街浪徒 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:17

    A feature-complete solution for cookies

    The self-made solutions proposed in the other answers here don't cover a lot of special cases, can easily break and lack a lot of standard features, such as persistence.

    As mentioned by isaacs, the request module now has true cookie support. They provide examples with cookies on their Github page. The examples explain how to enable cookie support by adding a "tough-cookie" cookie jar to your request.

    NOTE: A cookie jar contains and helps you manage your cookies.

    To quote their Readme (as of April 2015):

    Cookies are disabled by default (else, they would be used in subsequent requests). To enable cookies, set jar to true (either in defaults or options) and install tough-cookie.

    The cookie management is provided through the tough-cookie module. It is a stable, rather feature-complete cookie management tool that implements the "HTTP State Management Mechanism" - RFC 6265. It even offers a variety of options to persist (store) cookies, using a cookie store.

提交回复
热议问题