Skip to main content

I’m developing an API on Node.js + express on Heroku, and every time I fetch anything with POST, I get a 400 Bad request error.


Postman requests to the same endpoint work perfectly. So the only hypothesis I have is that I need a ‘Host’ header in my Airtable request (when I remove the ‘Host’ header in Postman. I get the same 400 Bad request error), but I don’t know what this ‘Host’ header should be.


To reproduce:



  1. Create a simple Node.js+express app on Heroku where you return the same body as you get:


app.post('/', (request, response) => {
response.status(200).send(request.body);
})


  1. Create Airtable automation with fetch (or an app with remoteFetchAsync):


let response = await fetch('https://....herokuapp.com/api/', {
method: 'POST',
body: JSON.stringify('Hello World!'),
headers: {
'Content-Type': 'application/json; charset=UTF-8',
'Accept': 'application/json'
},
});
console.log(await response.text());

You’ll get a ‘400 Bad Request’ response.

Solved!


I had to add 'Host': 'https://*.airtableblocks.com' header.


await remoteFetchAsync('https://....herokuapp.com/api/', {
method: 'POST',
body: requestBody,
headers: {
'Content-Type': 'application/json; charset=UTF-8',
'Host': 'https://*.airtableblocks.com',
'Accept': 'application/json'
},
})

Reply