Manspof
Manspof

Reputation: 357

react native fetch http request throw error

I'm trying to make http request in react native app and it throws me an error

Uncaught Error: unsupported BodyInit type
at Response.Body._initBody (index.bundle?platform=android&dev=true&minify=false:13594)
at new Response (index.bundle?platform=android&dev=true&minify=false:13765)
at XMLHttpRequest.xhr.onload (index.bundle?platform=android&dev=true&minify=false:13820)

in index.js of the app I added this line

GLOBAL.XMLHttpRequest = GLOBAL.originalXMLHttpRequest || GLOBAL.XMLHttpRequest

fetch request

            const headers = {};
        headers['Accept'] = 'application/json';
        headers['Content-Type'] = 'application/json';
        let response = await fetch('https://www.saramashkim.co.il/api/get_all_product', {
            method: 'GET',
            headers: headers,
            body:  null,
        })

when I check this url in POSTMAN it works fine and I get all data..

Upvotes: 0

Views: 942

Answers (1)

Tejashwi Kalp Taru
Tejashwi Kalp Taru

Reputation: 3074

Try the code below:

var headers = {'Accept': 'application/json', 'Content-Type': 'application/json'};
fetch('https://www.saramashkim.co.il/api/get_all_product', {
    method: 'GET',
    headers: headers,
}).then((response) => response.json())
  .then((responseJson) => {
    console.log('response', responseJson);
  })
  .catch((error) =>{
    console.error(error);
  });

You can see the response is logged to console. (I've updated my answer)

repl: https://repl.it/@tejashwikalptar/samplefetchtest

Screenshot: REPL Screenshot with API result

Upvotes: 1

Related Questions