Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user-agent is still the default after I set it in the header #614

Closed
ethanyuwang opened this issue Mar 28, 2018 · 2 comments
Closed

user-agent is still the default after I set it in the header #614

ethanyuwang opened this issue Mar 28, 2018 · 2 comments

Comments

@ethanyuwang
Copy link

ethanyuwang commented Mar 28, 2018

I'm using fetch on React Native both Android and iOS. I set the User-Agent to the name of the platform so I can distinguish between Android and iOS from the backend:

const URLENCODED_HEADER = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
  'User-Agent': Platform.OS + "/" + DeviceInfo.getUniqueID().toString()
}

export async function doRegister(secureInfo) {
  formBody = encodeParameters(secureInfo)
  try {
    let response = await fetch(SERVER_URL+'/user/register', {
      method: "POST",
      headers: URLENCODED_HEADER,
      body: formBody,
      credentials: 'include'
    });
    let responseJson = await response.json();
    return responseJson;
  } catch(error) {
    console.error(error);
    throw error;
  }
}

I have logged the requests in Reactotron and it looks like user-agent was changed successfully:

screen shot 2018-03-22 at 3 12 07 pm

However, on the server side, the ua logged from the requests is still the default for android and ios devices, such as below:

Mozilla/5.0 (iPad; CPU OS 11_2_6 like Mac OS X) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0 Mobile/15D100 Safari/604.1

okhttp/3.6.0

Dalvik/2.1.0 (Linux; U; Android 5.1.1; Coolpad 3622A Build/LMY47V)

So is it possible to change the user-agent with fetch in React Native? If so what's the proper way to do it?

@mislav
Copy link
Contributor

mislav commented Mar 29, 2018

Browsers will typically disallow you from changing the User-agent string. It's a security restriction. There is no way around it; you'll have to use a different header name, e.g. X-Custom-user-agent, and configure the server to read it.

@mislav mislav closed this as completed Mar 29, 2018
@ericksprengel
Copy link

ericksprengel commented Feb 11, 2019

@ethanyuwang Try to Stop Remote JS Debug to test it.

I was dealing with the same problem too.
It's working for me. :)

Some notes:

  1. it's only for React Native
  2. React Native already has a default User-Agent. Something like: <PROJECT_NAME>/<VERSION> CFNetwork/975.0.3 Darwin/17.7.0
  3. while using Remote JS Debugging, it'll use the browser user-agent. Something like: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants