React Native Fetch Api Unable to Set Cookie Header

百般思念 提交于 2021-01-05 09:30:26

问题


I am developing a react native app and php rest service as backend.

When user login i echo user and session id info as $response array with json_encode() function.

$response['user'] stores user info. $response['sessid'] stores session id.

In Postman i can set a Header as Cookie and as value i use sessionid. It works perfectly. But in fetch api i can't set Cookie header.

Here is my code:

const value = await AsyncStorage.getItem('sessid');
console.log("value" + value)
var url = 'http://192.168.1.27/zuper/test.php';
fetch(url, {
  method: 'GET', 
  headers:{
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Cookie' : value
  }
}).then(  (response) => {
  console.warn(response)
 })

This is value of value variable : 0d580f8b8e1e1ead1ce038cb12289a57

Response for this fetch comes with a php error which says

user is not defined

from line $_SESSION['user']['name']


回答1:


You are correct. Cookie is listed amoung the forbidden header names:

These are forbidden so the user agent remains in full control over them.

If you want to send a Cookie in an HTTP request, then you must make a prior HTTP request to the same origin that uses a Set-Cookie response header to set it.



来源:https://stackoverflow.com/questions/51855090/react-native-fetch-api-unable-to-set-cookie-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!