ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.58k stars 1.33k forks source link

How can I capture 302? #1806

Open zystudios opened 4 months ago

zystudios commented 4 months ago

I request an API, this API will return 302, and automatically jump to another API, and then return data, but I see status code has been 200, I need to capture 302 to do some operations, what should I do?

thanks

woaer commented 4 months ago

You can use the following code:

http
  .get('http://example.com/')
  // following redirects
  .redirects(0)
  // error handling
  .ok(res => res.status >= 200 && res.status < 400)
  .then(res => {
    console.log(res.status)
    console.log(res.headers['location'])
  })

Reference documentation: Following Redirects: SuperAgent Documentation Error Handling: SuperAgent Documentation

zystudios commented 4 months ago

You can use the following code:

http
  .get('http://example.com/')
  // following redirects
  .redirects(0)
  // error handling
  .ok(res => res.status >= 200 && res.status < 400)
  .then(res => {
    console.log(res.status)
    console.log(res.headers['location'])
  })

Reference documentation: Following Redirects: SuperAgent Documentation Error Handling: SuperAgent Documentation

thx, I'll try later