0

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..

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

not sure, 'GET' method don't have any body.. so it's not the problem
Remove the body
CORS is not allowed on your URL side, so I am unable to test it, but on my local server, this is working fine
in my local server it works fine too. but I want it to work in react native app..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.