Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read header key value from the response header React Js

console snapshotI am calling one fetch API which giving the response header 'key-name':'value'. I checked in the chrome developer tools network tab. It showing the value. However, I am trying to read it during API response it's showing empty. I tried multiple ways but still not working. Below is my code.

const requestOptions = {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    pid: d,
                    cy: v
                }
                )
            };
                
  fetch('https://mysite/api/manage/', requestOptions)
                .then(res => res.json())
                .then(jsong => {
                    console.log(jsong);
            console.log(jsong.headers); 
                }).catch((err) => {
                    console.log(err);
                });

Please help. I added a console snapshot and network response snapshot as well. network response snapshot

like image 447
nitin kumar Avatar asked Sep 18 '25 06:09

nitin kumar


1 Answers

I got the solution and here is the answer. It might be useful for someone

fetch('mysite.com/api/manage-cart/', requestOptions)
            .then(res => res.headers.get('cart_info')
            )
            .then(res => {
                window.localStorage.setItem("cartData", JSON.stringify(res));
            }).catch((err) => {
                console.log(err);
            }); .

I am storing the value on local storage for further use.

like image 131
nitin kumar Avatar answered Sep 20 '25 22:09

nitin kumar