Trouble with Zapier POST request: 'body used already for' error

Hey everyone, I’m having some issues with a Zapier POST request. I’m trying to use the fetch function to make a POST request, but I keep getting this weird error message. It says ‘body used already for’ and then shows the URL I’m trying to use.

Here’s what my code looks like:

fetch('https://example.com/api', { 
    method: 'POST', 
    body: myData 
})
.then(response => {
    console.log(response.text());
    return response.text();
})
.then(data => {
    let result = { success: true, response: data };
    finishUp(null, result);
})
.catch(finishUp);

When I run this, I get a red error box with that message. I’m not sure what it means or how to fix it. Has anyone run into this before? Any ideas on what might be causing it or how to solve it? Thanks in advance for any help!

In my experience, the error you are encountering with the Zapier POST request arises because the response body is being read more than once. When you call response.text(), it consumes the stream, making it unavailable for a second read. A better approach involves retrieving the response body a single time and then storing it, which prevents the error.

I addressed a similar problem by ensuring that the response is processed only once. Additionally, if myData is an object, I ensure it is passed in the proper format by stringifying it and setting the correct Content-Type header. This method reliably resolved the issue.

I’ve encountered this issue before, and it can be quite frustrating. The error you’re seeing typically occurs when you try to read the response body more than once. In your code, you’re calling response.text() twice - once in the console.log and again in the return statement.

To fix this, you should only call response.text() once and store the result. Here’s how you can modify your code:

fetch('https://example.com/api', { 
    method: 'POST', 
    body: myData 
})
.then(response => response.text())
.then(data => {
    console.log(data);
    let result = { success: true, response: data };
    finishUp(null, result);
})
.catch(finishUp);

This way, you’re only reading the response body once. Also, make sure your myData is properly formatted for the API you’re calling. If it’s an object, you might need to stringify it and set the correct content-type header.

Hope this helps solve your issue!

hey there! i ran into this too. the problem is ur reading the response twice. try this:

let responseText;
fetch(‘https://example.com/api’, {
method: ‘POST’,
body: JSON.stringify(myData),
headers: {‘Content-Type’: ‘application/json’}
})
.then(response => responseText = response.text())
.then(data => {
console.log(responseText);
finishUp(null, { success: true, response: responseText });
})
.catch(finishUp);

this should fix it!