Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch: Can you pass parameters to the server

Tags:

fetch

I'm using a basic fetch to obtain data from an express server that queries the data from a database. So I want to do a login system, i want to send the user/password from the input fields with the fetch request. so i can perform the logical check to see if password matches the data in the server. and respond with the result. Is it possible to do a fetch that can pass parameters?

Updated Code Below:

let fetchData = {
  method: "post",
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },

  body: JSON.stringify({
    username: this.state.user,
    password: this.state.password
  })
}

var fromServer = fetch('http://localhost:3000/', fetchData)
.then(function(response){
  if( !response.ok){
    throw Error (response.statusText);
  }
  return response.json();
})
.then(function(response) {
  console.log(response);
})
.catch(error => console.log("there was an error --> " + error));

Express function

app.post('/', function(req, res){
  var uname = req.body.username;
  var pw = req.body.password;
  console.log(uname);
  console.log(pw);
});
like image 825
Eric Avatar asked May 29 '17 02:05

Eric


People also ask

How do you pass parameters in Fetch?

To send query parameters in a POST request in JavaScript, we need to pass a body property to the configuration object of the Fetch API that matches the data type of the "Content-Type" header. The body will always need to match the "Content-type" header.

What are the different ways to pass parameters to the server?

You can pass them via a URL, a query string, a request header, a request body, or even a form.

What is the correct way to get data using fetch method?

Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.

What is difference between Axios and fetch?

Differences between Axios and Fetch:Axios has url in request object. Fetch has no url in request object. Axios is a stand-alone third party package that can be easily installed. Fetch is built into most modern browsers; no installation is required as such.


1 Answers

Of course you can!

Here is an example of Fetch using a POST request:

fetch("http://example.com/api/endpoint/", {
  method: "post",
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },

  //make sure to serialize your JSON body
  body: JSON.stringify({
    name: myName,
    password: myPassword
  })
})
.then( (response) => { 
   //do something awesome that makes the world a better place
});

I've answered this question before, check this for more details:

POST Request with Fetch API?

To process the request in Express, as mentioned in your comment assuming your express server is setup to parse body requests, you can do this:

app.post('/your/route', function(req, res) {
    var name= req.body.name;
    var password = req.body.password;

    // ...do your bidding with this data
});
like image 127
hellojebus Avatar answered Oct 08 '22 01:10

hellojebus