I'm having problems translating the following CURL request into my NodeRED flow:
curl -X POST -u "apikey:my_api_key" -F "features=objects" -F "collection_ids=my_collection_ids" -F "image_url=my_image_URL" "API_url"
I created a function node and a HTTP request node (with the method set to POST and the URL set to "API_url").
In the function node I wrote the following code:
msg.headers = {}
msg.headers = {
'content-type': 'application/x-www-form-urlencoded',
'apikey': 'my_api_key'
};
msg.payload = { }
msg.payload = {
'features': 'objects',
'collection_ids': my_collection_ids',
'image_url': my_image_URL'
};
return msg;
When I trigger the request, I get this error:
"{"code":401, "error": "Unauthorized"}"
am I doing a mistake passing the apikey in the header? any suggestions? thanks a lot in advance
The issue is with your authentication header.
When you pass -u username:pass
to curl, it generates the appropriate Authorization
header in the request which looks like:
Authorization: Basic dXNlcm5hbWU6cGFzcw==
This has the username:pass
part encoded using base64.
So for your case, you should do the following to set the header properly:
msg.headers = {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic '+(new Buffer('apikey:my_api_key')).toString('base64')
};