WebView does not send custom request header after the first initial page load
The source prop for WebView allows for passing request headers. When the webview renders for the first time, the request headers are sent.
But when the links within that page are clicked, the headers are not sent to the server in the request header. Similarly, when the goBack() method is called on the WebView, the request headers are not being sent.
+1. I got the same problem with version 30-rc
And it only exists on Android. My iOS app works normally.
We are seeing the problem on iOS
Any update on this? Facing the problem on both iOS and Android. Or anyone can point me in the right direction on how to fix this?
Reliable HTTP headers are important for many apps / APIs to work. Any update on this?
+1
Can you provide a sample rnplay.org app that reproduces this problem?
I'll see if @chiraggshah can provide a sample from our application however he's on holiday for the next 2.5 weeks.
react-native-cli: 1.3.0 react-native: 0.38.1
Android has the same problem, not test iOS.
I was wondering if anyone has a workaround or fix for this issue?
@alkhanna : I have written a blog with the workaround for this. http://blog.bigbinary.com/2016/07/26/passing-request-headers-on-each-webview-request-in-react-native.html
@lacker : I am not sure how to host own server which logs the headers received on each request using rnplay.org. Instead, I can paste a sample app here.
Server:
var http = require('http');
var port = 9000;
function logRequest(request) {
console.log("Processing request for: ", request.url)
console.log("Custom Header: ", request.headers["custom-app-header"])
console.log("Request Processed\n")
}
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
switch(request.url) {
case "/":
response.write("<html><body>Welcome<a href='/bye'>Bye</a></body></html>");
logRequest(request)
break;
case "/bye":
response.write("<html><body>Bye<a href='/'>Welcome</a></body></html>");
logRequest(request)
break;
default:
break;
}
response.end();
}).listen(port);
Webview Code:
<WebView
source={
{
uri: "http://localhost:3000",
headers: {"custom-app-header": "react-native-ios-app"}
}
}
/>
After starting the node server, the logs show the headers being passed for the first request sent by webview. But on subsequent page clicks, the header is not sent.
Let me know if I can be more descriptive, and I will do as required.
+1
@satya164 : Can you point me to the right person who possibly would have the insight on how to fix this? This is a long time issue and has not received much attention.
Or someone can guide me on how to fix it or where to start looking? That too would be a good start.
+1
+1
According the docs
source?: {uri: string, method: string, headers: object, body: string}, {html: string, baseUrl: string}, number
Loads static html or a uri (with optional headers) in the WebView.
This implies that only the first source uri request of the WebView will use the custom headers... So, dumb question... but does this even suppose to work like you expect?
If you're iOS only, you can use this workaround (use as prop of your WebView, I assume you store the webview's url in this.state.source). Yeah, it sucks... hopefully this gets fixed soon.
onShouldStartLoadWithRequest={(request) => {
console.log('**** REQUEST.url: ' + request.url + '\n**** REQUEST.navigationType: ' + request.navigationType);
if (request.url === this.state.source) {
return true;
}
// HORRENDOUS hack: RN's WebView doesn't bother keeping headers around.
// So we always fail new requests, then update state to re-render with new URL
// NOTE: only works on iOS
this.setState({ source: request.url });
return false;
}}
@xaphod : You can use the workaround I posted above. That works for both iOS and Android
@chiraggshah Thanks, yes I saw your workaround. It has several disadvantages for me, chiefly hitting all endpoints twice. That's why I used a different workaround