Fetch, passing parameters to a POST request

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

To get a response, I must pass the following data:

email: '[email protected]', 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': '[email protected]',
        '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"}

Author: Виктор Меладзе, 2018-05-12

1 answers

curl'om just sent a request:

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

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

So, you need to send your email and password in the body, not in the headers.

fetch('https://mysterious-reef-29460.herokuapp.com/api/v1/validate', {
    method: 'post',
    body: JSON.stringify({email: '[email protected]', password: '12345'}),
    headers: {
        'content-type': 'application/json'
    }
})
 5
Author: Suvitruf - Andrei Apanasik, 2018-05-12 18:33:44