How to customize HTTP_USER_AGENT header with Graphene and React Native?

Viewed 916

I found that React Native does not give HTTP_USER_AGENT value to Django Graphene. On Garaphene, I receive the header by:

def resolve(cls, root, info, **kwargs):
    info.context.META['HTTP_USER_AGENT']

It works when I send a query through a browser. However, the HTTP_USER_AGENT only has "okhttp/3.6.0" value when I send a query through my React Native app. So I tried to change the HTTP_USER_AGENT value by setting headers of axios.

onSubmit = async () => {
        if (!this.state.isSubmitting) {
            this.setState({isSubmitting: true});
            await axios({
                url: 'http://192.168.0.6:8000/graphql/',
                method: 'post',
                headers: {
                    HTTP_USER_AGENT: 'CCC'
                },
                data: {
                    query: `

It does not work. HTTP_USER_AGENT does not change and only shows "okhttp/3.6.0". I tried other custom headers and found that they do not work either. I cannot just set my own custom-named-header. Why is this the case? The only header I was available to change was AUTHORIZATION. Django Graphene received the header value when I set

await axios({
                    url: 'http://192.168.0.6:8000/graphql/',
                    method: 'post',
                    headers: {
                        AUTHORIZATION: 'JWT CCC'
                    },

and the value was available from

info.context.META['HTTP_AUTHORIZATION']

I had no idea if my assumptions were wrong.

1 Answers

Solved it luckily.

You have to set 'USER-AGENT' header on your client side(React Native)

await axios({
                url: 'http://192.168.0.6:8000/graphql/',
                method: 'post',
                timeout: 5000,
                headers: {
                    'USER-AGENT': await Constants.getWebViewUserAgentAsync(),
                },

And receive the value as HTTP_USER_AGENT on your server side (Django Graphene)

ua_string = info.context.META['HTTP_USER_AGENT']
        user_agent = parse(ua_string)

I do not know why their names are different (it took me half a day) but this is how it is.

Related