invalid json response body error with Express node-fetch using formData invalid json response body error with Express node-fetch using formData express express

invalid json response body error with Express node-fetch using formData


The body is consumed after calling .json(), not sure you can access it afterwards. A simple workaround: get the raw body and parse it yourself:

async function safeParseJSON(response) {    const body = await response.text();    try {        return JSON.parse(body);    } catch (err) {        console.error("Error:", err);        console.error("Response body:", body);        // throw err;        return ReE(response, err.message, 500)    }}const json = await = fetch(...).then(safeParseJSON)


Try res => console.log(res) in your first .then() block to see what the response is. Usually that error "Unexpected token < in JSON..." means that the response returned some html and the "<" in the error is an opening tag.