0

i am trying to fetch data using GET method for an Api. it returns data in json in ios but in android it return html Script as shown in image,

enter image description here

 let response = await axios.get('https://xxxxxxxx.com/api/reactappsettings/react_get_all_settings/?insecure=cool',
    {headers:{
      'Content-Type': 'application/json;charset=UTF-8',
      'Accept' : 'application/json',
      "Access-Control-Allow-Credentials": true,
    },withCredentials:true})
  
     console.log("======>",response);

i am stuck here due to this issue, any solutions?

i also tried using react-native-cookie to handle cookies .

CookieManager.get('https://mvhardhats.com')
    .then(async (res) => {
      console.log('CookieManager.get =>', res);
       await axios.get(
        `https://mvhardhats.com/api/reactappsettings/react_get_all_settings/?insecure=cool`,
        {
          headers: {
             Cookie:`visid_incap_2485071=${res.visid_incap_2485071}; incap_ses_882_2485071=${res.incap_ses_305_2485071}`,
          },
          withCredentials:true
        },
      ).then((data)=>{
        console.log(data)
      })
       // => 'user_session=abcdefg; path=/;'
    })

but still it returns html even after i got coockies.

  • This is because you have been blocked by Incapsula's WAF. There are ways to get around it but if you have access to the Incapsula account the best way is to create a whitelist rule to allow your application to send requests. – jaxwilko Mar 13 '21 at 07:37

1 Answers1

0

Check the whitelist rule in your application and try this code:

Need to use .json() on the response.

let response = await axios.get('https://mvhardhats.com/api/reactappsettings/react_get_all_settings/?insecure=cool', {
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  }
}).then((response) => response.json())
.then((response) => {
  console.log("======>",response);
});
Zoe
  • 27,060
  • 21
  • 118
  • 148
Anuj
  • 640
  • 7
  • 26
  • Gives me this kind of error. SyntaxError: Unexpected token < in JSON at position 0 at parse () at tryCallOne (core.js:37) at core.js:123 at JSTimers.js:277 at _callTimer (JSTimers.js:135) at _callImmediatesPass (JSTimers.js:183) at Object.callImmediates (JSTimers.js:446) at MessageQueue.__callImmediates (MessageQueue.js:396) at MessageQueue.js:144 at MessageQueue.__guard (MessageQueue.js:373) – Jigar Prajapati Mar 13 '21 at 08:08
  • It showing me somekind of error related to cookie. so i made changes using react-native-cookie. – Jigar Prajapati Mar 13 '21 at 09:13
  • You're receiving HTML (or XML) back from the server, but the dataType: json is telling jQuery to parse as JSON. Check the "Network" tab in Chrome dev tools to see contents of the server's response. Ask backend developer about the response he is sending to you. – Anuj Mar 13 '21 at 17:41