I want to call curl with NodeJS with GET method. I want to pass authorization token and parameter. I've wrote my codes as below:
const url = 'https://external-url?table-name=LD_PRD&schema-name=REF_SCHEMA';
const header = 'Basic XXXXXXNzI=';
const curlCommand = `curl -s GET -H "Authorization: ${header}" ${url}`;
try {
// Promisify the exec function to use it with async/await
const execPromise = util.promisify(exec);
const { stdout, stderr } = await execPromise(curlCommand);
if (stderr) {
// Curl might produce an error on stderr even if execPromise succeeds
throw new Error(`Curl stderr: ${stderr}`);
}
const data = JSON.parse(stdout);
//console.log('Success:', data);
return {
statusCode: 200,
body: JSON.stringify(data),
};
} catch (error: any) {
console.log("======== ERROR =========", error);
return {
statusCode: 500,
body: JSON.stringify({
message: 'Failed to execute curl command',
error: 'Failed to execute curl command',
}),
};
}
But I see my curl is having error and showing 500:
{
"message": "Failed to execute curl command",
"error": "Failed to execute curl command"
}
In the console, I see the following:
'schema-name' is not recognized as an internal or external command, operable program or batch file.
`exec` starts a shell and executes what ever you pass over.
Your command evalutes to
curl -s GET -H "Authorization: Basic XXXXXXNzI=" https://external-url?table-name=LD_PRD & schema-name=REF_SCHEMA
Your `&` in the URL tells the shell "this are two commands".
Put your url in quotes, and it should work:
const url = 'https://external-url?table-name=LD_PRD&schema-name=REF_SCHEMA';
const header = 'Basic XXXXXXNzI=';
const curlCommand = `curl -s GET -H "Authorization: ${header}" "${url}"`;
Your shell tries to execute schema-name as executuable because of the & in the URL