how to put –data in GET method ? since GET not recieving body parameter

i’ve given the api endpoint with GET method but i think it needs a body, when i test it on postman it works fine but in react native when i try to fetch it it shows error [TypeError: Body not allowed for GET or HEAD requests]

my backend partner send this curl, how to use the –data since GET are not recieving any body

    curl --request GET \
  --url http://base_url/api/v2/order/all \
  --header 'Content-Type: application/json' \
  --cookie 'token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwaG9uZU51bWJlciI6IjA4ODc3NzA5MjIxIiwidXNlcm5hbWUiOiJGbG8iLCJpYXQiOjE2NTEwMzIxNTYsImV4cCI6MTY1MTA3NTM1Nn0.JkwTPvjig7bd8Q27MvZ7DsUCz68Qyzh3EctFTRh-m0E; connect.sid=s%253AgtaL-l_60sBGAdEhTiHspbzX3rBBiEFg.O5z0JBi7Oqo1UXSZOxQckm2FNhG3A%252BWZod951CC5Cys' \
  --data '{
    "userId":"79025884",
    "limit":10,
    "page":1
}'

enter image description here

this is my function

function GetActivity() {
    const url = APIConfig.SERVER.ORDER + "/all";
    fetch(url, {
      method: "GET",
      headers: { "content-type": "application/JSON" },
      body: JSON.stringify({
            userId: "79025884",
            limit: 10,
            page: 1,
           }),
    })
      .then((response) => response.json())
      .then((data) => {
        console.log("GetActivity Order:", data);
        setOrderList(data.data);
      })
      .catch((error) => {
        console.error("Error:", error);
      });
  }

>Solution :

For a GET request, any parameters you want to pass to the API end point will need to be sent as part of the url I believe.

E.g. http://example.com/id/1 (where 1 is the dynamic value for the ID parameter)

I think the error you are seeing is because your trying to set a "body" value for a get request, which would be used with a POST request instead for example.

Leave a Reply