0

Do you how to make a POST request from React of the following:

http --auth name:password POST http://localhost:5000/api/tokens

My current attempt to make a request with React is as follow:

const requestOptions = {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        name: 'name',
        password: 'password'
    })
};
fetch('/api/tokens', requestOptions).then(res => res.json());

Thanks

Gerard Bro
  • 107
  • 3
  • 2
    https://stackoverflow.com/a/35780539/3930247 – technophyle Nov 26 '20 at 16:57
  • Thank for the page, but I don't see how I can link this to my issue – Gerard Bro Nov 26 '20 at 17:10
  • `'Authorization': 'Basic '+btoa('username:password'),` you can try this part. – technophyle Nov 26 '20 at 17:18
  • Thank you, that works. But when the authentification is wrong, the browser gives me a sign in dialog box, how do I get rid of that? I've tried to handle this in .then() or .catch(), but it seems like the dialog box is appearing before .then() is activated. – Gerard Bro Nov 26 '20 at 18:37
  • You can do it with the catch block. Sounds like your app already has some fallback exception handling. – technophyle Nov 26 '20 at 18:41
  • As I explained: the dialog appear before the catch block is being executed. Here's my code: `fetch('/api/tokens', requestOptions).then(res => res.status).catch((err) => {alert(err.status); return err.status;});` The catch block isn't being taken into account. – Gerard Bro Nov 26 '20 at 18:48

0 Answers0