I have a POST request: ' https://mysterious-reef-29460.herokuapp.com/api/v1/validate '

To get an answer, I have to pass such data:

email: 'max@test.com', password: '12345', content-type: 'application/json' 

I tried to do this:

 const status = response => { if (response.status !== 200) { return Promise.reject(new Error(response.statusText)) } return Promise.resolve(response) } const json = response => { console.log(response.headers.get('content-type')); return response.json() } fetch('https://mysterious-reef-29460.herokuapp.com/api/v1/validate', { method: 'post', body: 'test=1', headers: { 'email': 'max@test.com', 'password': '12345', } }) .then(status) .then(json) .then(data => { console.log('data', data); }) .catch(error => { console.log('error', error); }) 

But it returns: {status: "err", message: "wrong_email_or_password"}

1 answer 1

Just curl sent a request:

 curl -v -H "Content-Type: application/json" -H "content-type: application/json" -X POST -d "{\"email\":\"max@test.com\",\"password\":\"12345\"}" http://mysterious-reef-29460.herokuapp.com/api/v1/validate 

And I received in the answer: {"status":"ok","data":{"id":1}} .

So, mail and password must be sent in the body, not in the headers.

 fetch('https://mysterious-reef-29460.herokuapp.com/api/v1/validate', { method: 'post', body: JSON.stringify({email: 'max@test.com', password: '12345'}), headers: { 'content-type': 'application/json' } }) 
  • Ah, literally a minute late with an absolutely identical answer. Well, who wrote such a statement of work, that there is not a single mention of how exactly you need to send data to the server and in what format :) - JamesJGoodwin
  • @JamesJGoodwin it says that "POST request" and "when you press the button", which implies that the data is sent in the body of the POST request. - Suvitruf