I’m having issues with a Zapier POST request. I’m using the fetch method to make the request, but I keep getting an error message. Here’s my code:
fetch('API_ENDPOINT', {
method: 'POST',
'body': payload
})
.then(response => {
console.log(response.text());
return response.text();
})
.then(textData => {
let result = { success: true, data: textData };
finishProcess(null, result);
})
.catch(finishProcess);
When I run this, I get a red error box saying:
Error: body used already for: API_ENDPOINT
What does this mean? How can I fix it? I’m pretty new to working with APIs in Zapier, so any help would be great. Thanks!
hey, i had the same issue. try reading the response just once by saving it in a var before logging. also, check your body syntax in fetch. hope it helps!
I’ve encountered this issue before, and it can be frustrating. The error you’re seeing is because the response body is being consumed twice. Here’s what worked for me:
Instead of calling response.text() twice, store it in a variable first. Also, make sure your fetch options are correct. Try this modified version:
fetch('API_ENDPOINT', {
method: 'POST',
body: payload
})
.then(response => response.text())
.then(textData => {
console.log(textData);
return { success: true, data: textData };
})
.then(result => {
finishProcess(null, result);
})
.catch(finishProcess);
This approach should resolve the ‘body used already’ error. Remember to properly handle any potential JSON parsing if your API returns JSON data. Let me know if you need any more clarification!
That error usually pops up when you’re trying 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. The response body can only be consumed once.
To fix this, you could modify your code to store the text result in a variable:
fetch('API_ENDPOINT', {
method: 'POST',
body: payload
})
.then(response => response.text())
.then(textData => {
console.log(textData);
let result = { success: true, data: textData };
finishProcess(null, result);
})
.catch(finishProcess);
This way, you’re only reading the response body once, which should resolve the error. Also, make sure ‘body’ isn’t in quotes in your fetch options. Hope this helps!