How can I send data through fetch API by GET request?

16,988

Solution 1

GET requests do not support bodies, and parameters must be sent in the URL as you have shown in your example. If you're not able to see them on the server-side, it may be an issue with the server which should be asked as a separate question.

As a sanity test, navigate to that URL in a browser (or a tool like Postman) and test that the result is correct, before implementing the call with fetch, so that you'll at least confirm if it's a server issue or a JavaScript issue.

Solution 2

Try escaping the @ character with %40 or doing something like {fetch("http://192.168.90.53:4000/game/?email=" + encodeURIComponent('[email protected]'), {method: "GET"} )...

Share:
16,988
Aditya Rohilla
Author by

Aditya Rohilla

Computer Science Grad Student at Arizona State University. Previously Software Engineer at Samsung Research Institute, Delhi. [Python, Android, Machine Learning, Data Mining, C++, Java, Javascript, React-Redux, Node]

Updated on July 27, 2022

Comments

  • Aditya Rohilla
    Aditya Rohilla almost 2 years

    I want to send some data via fetch API to a server so that it can query a DB based on that. Unlike Ajax i can't find any suitable method to do so. Here is my fetch call:

    {fetch("http://192.168.90.53:4000/game/[email protected]", {method: "GET"} )
            .then((response) => response.json())
            .then((responseData) => {
                id= responseData.id;
                name= responseData.name; 
                console.log(responseData);
            })
           .catch((error) => {
                 console.warn(error);
               }); 
      }
    }
    

    I want to send a parameter such as email, so that the server can query the DB using that. I am using react-native android hence can't use an Ajax request. I want to use GET method only. Currently, the query params I'm passing in the URL aren't shown by the server in req.query

  • Aditya Rohilla
    Aditya Rohilla over 8 years
    I don't want to use POST method, I want to send query params with GET method. Currently, though i'm sending params with URL. The server isn't showing it in req.query. What to do?
  • Liem Ly Quan
    Liem Ly Quan over 8 years
    In that case, have you tried to navigate to that URL as John Shammas? If not working, can you try with some other URL, rather than local server (just guessing since I recently having trouble with localhost)
  • Ebram Shehata
    Ebram Shehata almost 4 years
    GET requests DO support bodies. Check: tools.ietf.org/html/rfc7231#page-24 also check ElasticSearch page about this elastic.co/guide/en/elasticsearch/guide/current/…