I have this serverless function, which acts like a proxy on Digital Ocean. The problem I’m encountering is that when I use fetch, I need to include a body. Without a body, I receive a 500 error. This setup works in Postman, but it doesn’t work in Node 18
async function main(args) {
const icc = args.icc;
console.log(icc);
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXX");
const raw = JSON.stringify({
"is_global": "true"
});
const requestOptions = {
method: "GET",
headers: myHeaders,
body: raw,
redirect: "follow"
};
const response = await fetch("http://000.0.00.000/api/icc/8934076500006252043/diagnostic?is_global=true", requestOptions)
const status = await response.text()
return {
body: { status },
}
}
With that code, I’m getting this error:
{
"message": "Request with GET/HEAD method cannot have body.",
"name": "TypeError",
"stack": "TypeError: Request with GET/HEAD method cannot have body.n at Object.fetch (node:internal/deps/undici/undici:11457:11)n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)n at async NodeActionRunner.main [as userScriptMain] (eval at <anonymous> (/nodejsAction/runner.js:93:27), <anonymous>:21:20)"
}
If I change for a post o add params, i get
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">n<title>405 Method Not Allowed</title>n<h1>Method Not Allowed</h1>n<p>The method is not allowed for the requested URL.</p>n