Answers
- The problem is that the
fetch
function expects the body to be instring
format. The corrected call is as follows:fetch('http://localhost:17525/api/person', { method: 'post', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ firstName: 'Fred' surname: 'Smith' }) })
- The problem is that the response body cannot be accessed directly in the response like this. Instead, the response's
json
asynchronous method should be used:const res = await fetch('http://localhost:17525/api/person/1'); const body = await res.json(); console.log('firstName', body.firstName);
- The problem is that the
catch
method is for network errors and not HTTP request errors. HTTP request errors can be dealt with in the...