Is it possible to modify the HTTP request headers of a React Native WebView component on iOS?
I'm aware of the onShouldStartLoadWithRequest
function but this doesn't seem to allow any possibility for modification.
Is it possible to modify the HTTP request headers of a React Native WebView component on iOS?
I'm aware of the onShouldStartLoadWithRequest
function but this doesn't seem to allow any possibility for modification.
You can put your request header on attribute headers
like this:
<WebView source={{
uri: "http://blog.apentle.com/",
headers: {
Authorization: "Basic YXBlbnRsZS5jb206YXBlbnRsZQ==",
}
}}
style={styles.webview} />
Yes, here is solution that works also for also for subsequent calls in the WebView - you can track the current URL, intercept new page loads, and navigate to them yourself (original credit for this technique to Chirag Shah from Big Binary)
const CustomHeaderWebView = (props) => {
const { uri, onLoadStart, ...restProps } = props;
const [currentURI, setURI] = useState(props.source.uri);
const newSource = { ...props.source, uri: currentURI };
return (
<WebView
{...restProps}
source={newSource}
onShouldStartLoadWithRequest={(request) => {
// If we're loading the current URI, allow it to load
if (request.url === currentURI) return true;
// We're loading a new URL -- change state first
setURI(request.url);
return false;
}}
/>
);
};
<CustomHeaderWebView
source={{
uri: 'http://example.com',
headers: {
'my-custom-header-key': 'my-custom-header-value',
},
}}
/>;