How the agent method in superagent saves cookie

newcomers ask for help. Now you use superagent on node to send a request to access several ports. You need to save the cookie obtained by the first request and let the later request continue to use. It just happens that there is a special agent () method in this library in the official document, but the example given in the official document is not very clear

.
const agent = request.agent();
agent
  .post("URL")
  .then(() => {
    return agent.get("/cookied-page");
  });

try printing agent as if the results are all cookiejar objects? So I"d like to ask what this / cookied-page is. Now I want to save the cookie returned by the request in a global variable.

Mar.09,2021

In Node SuperAgent does not save cookies by default, but you can use the .agent () method to create a copy of SuperAgent that saves cookies. Each copy has a separate cookie jar.

means that requests made with agent objects will retain cookie. / cookied-page is just an example of url,. You can change it to https://codeshelper.com/. The object returned by
request.agent () is the copy (backup) of request , so using API is the same as request. The original request.post ('/ api/pet') can be changed to use agent.post ('/ api/pet') , and agent will save cookie, and bring it to the next request.
for example, if you log in first:

  cookiejar  this package is probably  agent.jar.getCookie ("loginToken", {path: "/"}). Value ). 

Menu