Ehsan Ali
Ehsan Ali

Reputation: 1432

Remove some cookie from ajax call with Axios

I use Axios in browser to call ajax request. Now I have a problem with some cookie that has high priority than some header. Per request I send a header as AUTHTOKEN but in cookie SESSIONID key stored that high priority than AUTHTOKEN header. In some scenario, I need to ignore cookie. This is my code:

axios({
    url:`${sdpBasicUrl}/api/v3/requests/27363`,
    method: 'get',
    headers: {
        'Content-Type': 'application/json'  
        'AUTHTOKEN': 'GHG23847923HGJ'               
    }
})
.then(res => {
    console.log(res.data);
});

and this is cookie sample:

_z_identity=true; PORTALID=1; csrfcookie=aasdasdjh24234b2bjh4hjl; SESSIONID=ffd68d32a14841c99905e3cf4897e15ec9b4777020854a76821fd7e1eab6db2dcab482eb4cfea2ce7f5a6c47c80271d09f608ed985004e5c85681b2939681b18

What should I do? Do you have any solution to solve my problem?

Upvotes: 14

Views: 5385

Answers (2)

Parth Patel
Parth Patel

Reputation: 129

You can use transformRequest to modify the header for some requests. transformRequest allows changes to the request data and header before it is sent to the server. This is only applicable for request methods 'PUT', 'POST', 'PATCH' and 'DELETE'.

transformRequest: [function (data, headers) {
    // Modify the header here and return the header
    return data;
}],

You can get more information about it on https://axios-http.com/docs/req_config

Upvotes: 2

David Weber
David Weber

Reputation: 194

You are able to pass in cookies through the header like this:

Axios.request({
     url: "http://example.com",
     method: "get",
     headers:{
         Cookie: "cookie1=value; cookie2=value; cookie3=value;"
     } 
}).then...

So if you don't want the value to be there, you could override the values.

https://github.com/axios/axios/issues/943

Upvotes: 7

Related Questions