Skip to content

Instantly share code, notes, and snippets.

@mkdizajn
Created February 9, 2025 06:50
Show Gist options
  • Save mkdizajn/ee2218ee0aae4fa363ddb7c2f9a4875c to your computer and use it in GitHub Desktop.
Save mkdizajn/ee2218ee0aae4fa363ddb7c2f9a4875c to your computer and use it in GitHub Desktop.
javascript fetch two reqs one pass header cookie to another
// 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