-1

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);
      });
  }
CCP
  • 886
  • 1
  • 10
  • 30
  • Does this answer your question? [No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API](https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe) – mousetail Sep 09 '22 at 09:38
  • Seems like you are running into CORS, see the linked question for a more detailed explanation – mousetail Sep 09 '22 at 09:38
  • okay, i'll check it – CCP Sep 09 '22 at 09:39
  • i think it's not answering my question since it's POST method and using any other library that i don't know – CCP Sep 09 '22 at 09:44
  • It's the same thing. For POST it will reject the entire request while for GET it just disallows accessing the body. – mousetail Sep 09 '22 at 09:45
  • hey i update the question, can you check it again ? – CCP Sep 09 '22 at 09:50
  • You still have not enabled CORS like in the linked question – mousetail Sep 09 '22 at 09:51

3 Answers3

1

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.

Matt Woodward
  • 1,941
  • 20
  • 24
0

The same problem I faced several days ago, and I made some research about it. I can say that even though it is possible to use get method with body in postman you can't use get method with body with fetch or axios, because body is not allowed. And I think you should allow post method in your backend if your want to send data. You can read in depth about it here.

0

You need to add it to the URI or the HTTP request headers depending on the data. Usually URI path for hierarchical resource identification data and URI query for non-hierarchical. There are standard HTTP headers for many things including pagination. https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests They are for byte ranges, but you can use custom units like pages. Another way is doing pagination with URI.

GET https://example.com/api/v1/orders/by-user-id/79025884/?page=1&limit=10
GET https://example.com/api/v1/orders/by-user-id/79025884/ "Range: items=1-10"
inf3rno
  • 24,976
  • 11
  • 115
  • 197