Not Receiving Set-Cookie Header with axios post request

前端 未结 4 1627
囚心锁ツ
囚心锁ツ 2020-12-19 18:08

I have a PHP Script which successfully returns some simple Headers as well as a set-cookie header if called directly in the browser (or by postman). I can read the response-

4条回答
  •  时光取名叫无心
    2020-12-19 19:10

    This may not apply to your situation, but I had the same problem using axios in this standalone nodejs script.

    const config = {
        url: 'https://remote.url/login',
        method: 'post',        
        headers: {
            'content-type': 'application/x-www-form-urlencoded',
        },
        data: qs.stringify({
            'email': username,
            'password': pwd
        })
    }
    
    axios(config).then(res => {
        console.log(res.headers);
    }).catch(err => {
        console.log(err);
    })
    

    This returned http status 200 without set-cookie in the headers. With curl the header was correctly retrieved, but the status code was 302

    After adding the following config options to axios:

    maxRedirects: 0,
    validateStatus: function (status) {
        return status <= 302; // Reject only if the status code is greater than 302
      },
    

    I received the set-cookie in axios in the response.header.

    {
      server: 'nginx/1.16.1',
      date: 'Fri, 27 Dec 2019 16:03:16 GMT',
      'content-length': '74',
      connection: 'close',
      location: '/',
      'set-cookie': [
        'cookiename=xxxxxxxxxxxxxxxxxxxxx; path=/; expires=Sat, 26-Dec-2020 16:03:16 GMT'
      ],
      status: '302',
      'x-frame-options': 'DENY'
    }
    

    Without maxRedirects: 0 I received the html of the homepage of the remote url I used.

    Without validateStatus I received the set-cookie header in the err.response.headers object.

提交回复
热议问题