I'm trying to use an API that accepts the data as form data. In the PHP example I have been given, the working code is as follows using GuzzleHttp:
$response = $client->post('http://example.com', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken
],
'form_params' => [
'items' => $items
]
])
In my JS, I have the below code. According to the Request docs, using formData will parse an object into the correct format using the FormData library.
I've got this working manually using Postman. Im sending a x-www-form-urlencoded body with fields as:
item[0] = "abc"
item[1] = "def"
Can anyone see what I'm missing here as to why this isn't working?
const items = ["abc", "def"];
const response = await post({items: items}, "http://example.com")
exports.post = async function (data, url) {
const token = await getAccessToken();
const response = await request({
method: 'POST',
url: url,
headers:
{
Connection: 'keep-alive',
Authorization: `Bearer ${token.data.access_token}`,
Accept: 'application/json',
},
formData: data
});
return JSON.parse(response);
};