Created
February 9, 2025 06:50
-
-
Save mkdizajn/ee2218ee0aae4fa363ddb7c2f9a4875c to your computer and use it in GitHub Desktop.
javascript fetch two reqs one pass header cookie to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of obtaining a cookie (typically from a login endpoint) | |
fetch('http://localhost/api/v1/chatbot/chat', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({query: "hi - this is test one"}) | |
//body: JSON.stringify({email: "[email protected]", password: "12345!abcABC"}) | |
}) | |
.then(response => { | |
// Assuming the cookie is in the 'Set-Cookie' header | |
const cookie = response.headers.get('Set-Cookie'); | |
console.log(cookie); | |
// Use the cookie in the next request | |
return fetch('http://localhost/api/v1/chatbot/chat', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Cookie': cookie // Include the cookie in the headers | |
}, | |
body: JSON.stringify({query: "and what is this test req number please?"}) | |
}); | |
}) | |
.then(response => response.json()) | |
.then(data => console.log(data)) | |
.catch(error => console.error('Error fetching data:', error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment