问题
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