Today, I’m trying to send a POST request to a Freshdesk API but i’m not having much luck. I’ve managed to get it working using curl and Postman, but I can’t get it to work in Next.Js using the fetch method. My code is as follows:
import base64 from 'base-64'
export default async (req, res) => {
try {
const response = await fetch(
`https://endpoint.freshdesk.com/api/v2/tickets/28/reply`,
{
body: "We are working on this issue. Will keep you posted.",
headers: {
'Authorization': 'Basic ' + base64.encode("APIKEY:X"),
'Content-Type': 'application/json',
},
method: 'POST'
}
);
if (response.status >= 400) {
return res.status(400).json({
error: 'There was an error'
});
}
return res.status(200).json({ status: 'ok' });
} catch (error) {
return res.status(500).json({
error: 'There was an error'
});
}
};
When i try to POST this, it just gives me a 400 error, but i’m not sure why. Is my body incorrect? Maybe the headers are missing something?
Anyone with experience with Next.Js or Fetch who can help me?