valeriangalliat / fetch-cookie

Decorator for a `fetch` function to support automatic cookie storage and population. 🍪
The Unlicense
135 stars 29 forks source link

Any exaples #64

Closed scorpioRED closed 1 year ago

scorpioRED commented 3 years ago

Hi! I can't find any examples, maybe somebody can help me? I need to make a GET request to a login page, take a token from hidden input, then make a POS login request with a cookie from a previous request, then another request also should be with same session Something like that:

const loginRequest = await fetch(loginPageUrl, {method: 'GET'}).then(resp => resp.text())

const token = parseToken(loginRequest) // own parser function

const doLogin = await fetch(loginPageUrl, {method: 'POST', body: formData}).then(resp => resp.text()) 

if(doLogin == 'success'){
 const someAnotherRequest = await fetch(actionUrl, {method: 'POST', body: someData}).then(resp => resp.text())  

 this request available only if user is logged in
}
valeriangalliat commented 3 years ago

Hey! Did you wrap fetch like shown in the readme?

const nodeFetch = require('node-fetch')
const fetch = require('fetch-cookie')(nodeFetch)

If so the session should be shared between all the requests you're making.

You can try setting NODE_DEBUG=http in your environment (e.g. NODE_DEBUG=http node your-script.js or export NODE_DEBUG=http in your shell session) to see the raw HTTP debug information which will include the HTTP headers. Then you can verify if the cookies returned by the login response were indeed forwarded in the next request.

If they're not, it might be a bug from fetch-cookie, but I'll need you to share the debug logs (obfuscating sensitive parts of the cookies obviously) to take a look.