react-native icon indicating copy to clipboard operation
react-native copied to clipboard

WebView does not send custom request header after the first initial page load

Open chiraggshah opened this issue 10 years ago • 41 comments

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.

chiraggshah avatar Jul 11 '16 12:07 chiraggshah

+1. I got the same problem with version 30-rc

dunghuynh avatar Jul 17 '16 07:07 dunghuynh

And it only exists on Android. My iOS app works normally.

dunghuynh avatar Jul 17 '16 07:07 dunghuynh

We are seeing the problem on iOS

ghidinelli avatar Jul 18 '16 01:07 ghidinelli

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?

chiraggshah avatar Aug 23 '16 12:08 chiraggshah

Reliable HTTP headers are important for many apps / APIs to work. Any update on this?

ghidinelli avatar Sep 04 '16 07:09 ghidinelli

+1

karankurani avatar Nov 26 '16 10:11 karankurani

Can you provide a sample rnplay.org app that reproduces this problem?

lacker avatar Nov 30 '16 00:11 lacker

I'll see if @chiraggshah can provide a sample from our application however he's on holiday for the next 2.5 weeks.

ghidinelli avatar Dec 01 '16 01:12 ghidinelli

react-native-cli: 1.3.0 react-native: 0.38.1

Android has the same problem, not test iOS.

peichao01 avatar Dec 10 '16 03:12 peichao01

I was wondering if anyone has a workaround or fix for this issue?

alkhanna avatar Dec 28 '16 21:12 alkhanna

@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.

chiraggshah avatar Dec 29 '16 05:12 chiraggshah

+1

evanidul avatar Mar 02 '17 10:03 evanidul

@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.

chiraggshah avatar Apr 17 '17 18:04 chiraggshah

Or someone can guide me on how to fix it or where to start looking? That too would be a good start.

chiraggshah avatar Apr 17 '17 19:04 chiraggshah

+1

ismdcf avatar Jun 15 '17 09:06 ismdcf

+1

hmatijevic avatar Jun 20 '17 10:06 hmatijevic

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?

asimen1 avatar Jul 06 '17 11:07 asimen1

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 avatar Jul 31 '17 18:07 xaphod

@xaphod : You can use the workaround I posted above. That works for both iOS and Android

chiraggshah avatar Jul 31 '17 19:07 chiraggshah

@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

xaphod avatar Jul 31 '17 19:07 xaphod