fetch POST is returning HTTP 415, while curl goes on fine and returns result fetch POST is returning HTTP 415, while curl goes on fine and returns result curl curl

fetch POST is returning HTTP 415, while curl goes on fine and returns result


fetch() does not expect a JavaScript object at body. curl command and fetch() pattern are not the same. Use body: JSON.stringify(<javascript plain object>).

Request

Note: The body type can only be a Blob, BufferSource, FormData, URLSearchParams, USVString or ReadableStream type, so for adding a JSON object to the payload you need to stringify that object.

See Fetch with ReadableStream for status of implementation of ReadableStream set as value for body.


you must define Accept and Content-Type headers like that and stringify your data object

const params = {            headers: {                'Accept': "application/json, text/plain, */*",                'Content-Type': "application/json;charset=utf-8"            },            body: JSON.stringify(yourObject),            method: "POST"        };fetch(url, params)        .then(data => { console.log('data', data)})        .then(res => { console.log('res', res) })        .catch(error => { console.log('error', error) });