How can I make this API call from my Nodejs program? - node.js

A member of my team developed this API where you send some numeric values and it gives you back a probability. He deployed it in Heroku and sent me the task of connecting it with our backend, you can make the call from cmd like this:
curl -d "{\"Values\":[[value1,value 2,value 3,value 4,value 5]]}" -H "Content-Type: application/json" -X POST https://apibc1.herokuapp.com/predict
And it will work just like intended, but to be honest I don't know how to make this call in my server file, I'm trying to use the request package in Node but I keep getting the invalid URI error on my logs. An example of the API working from cmd:
And this is what happens when I make the same call in my server.js file:

If you have a working curl you can import it into postman and generate a working code sample for a lot of languages.
Import the curl request
Then click the code button on the right
and select a language/framework option from the dropdown

Related

how to implement separate handler for static file check authentication curl -tornado application

I am new to tornado web framework. I have below code.
static_path = pathlib.Path(pkg_resources.resource_filename("abcd.app.data", "web"))
self.application = tornado.web.Application(self.url_spec,
static_url_prefix="/v1/download/",
static_path=str(static_path))
So when I am doing CURL
curl -0 -o test_file -k https://99.76.65.87/v1/download/ab-linux-amd64
above command will heat v1/download URL and download ab-linux-amd64
Now My requirement is to implement authentication and authorization so that if any one is using CURL command It should first ask for username and password.
I have referred tornado webpage but no luck. (I am just thinking to create separate handler and that handler should be called when we request v1/download).
Can anyone help me the best way to implement it (Is it possible to use some inbuilt decorator like tornado.web.authenticated within self.application?)

Not able to publish flow to Node-RED via the HTTP API, but it works when doing it manually

I am trying to use the Node-RED API to publish a flow with this command:
curl -ss -X POST \
http://${IP_ADDRESS}:1880/flow \
-H 'content-type: application/json' \
--data-binary "#foo_nodered_flow.json"
(as per API docs here: https://nodered.org/docs/api/admin/methods/post/flow/)
but I get back this message:
{"error":"unexpected_error","message":"Error: duplicate id"}
The flow is more or less:
an MQTT subscriber
a function to transform messages in Javascript
a connection to InfluxDB to store these transformed messages (for this I have installed a custom npm package node-red-contrib-influxdb inside ~/.node-red/ https://flows.nodered.org/node/node-red-contrib-influxdb)
I don't find duplicate IDs in the field id when grepping with grep -nrI id foo_nodered_flow.json.
I was able to publish via HTTP POST a similar flow that was not involving InfluxDB but a simple file writer.
Why I am getting that error in the HTTP response? What's wrong with that flow?
How can I programmatically publish any flow using curl?
The file containing the flow has this structure:
{
"label": "Store redings - panel flow",
"nodes": [ <put here the flow you export from the Node-RED UI> ],
"configs": []
}
Make sure that the list of nodes is being cleaned from the node of type "type": "tab" containing the name of the flow e.g. "label": "Flow 1" (this label must go outside the nodes array as the first field as in the above sample).
I also tried to remove the top level id field and all the z fields which seem to be automatically replaced/fixed. But I still get the same errors.
I know this flow works because when I manually created it, then I was able to see the data flowing and being eventually stored inside InfluxDB.
Also when on the Node-RED UI (at http://${THE_IP_ADDRESS}:1880/) I can click on the "3 lines menu" > Import > Clipboard then paste the JSON description of the nodes of that flow and I am able to import the flow with no errors and then deploy the flow with no errors.
I am using:
node version: 10.14.2
Node-RED version: 0.19.5
node-red-contrib-influxdb version: 0.2.2

csrf token in bluemix deployed node instance

Hi I used the deploy to bluemix button at the top right of this page
https://alchemy-language-demo.mybluemix.net/?cm_mc_uid=69990450306114782046823&cm_mc_sid_50200000=1478206651
to create an Alchemy language node server. It runs ok -
https://alchemylanguage-nodejs-encekxdev-216.mybluemix.net/
but when I try to post to it using postman I get the response 'internal server error'.
Checking the server logs, it seems to respond to the request with
code: 'EBADCSRFTOKEN', error: 'invalid csrf token'
Even if I try to send the csrf from the webpage version of the site, it still doesn't work.
I feel like I have missed something in the configuration of the server but don't know what. I'm not great with servers etc so sorry if this is super basic.
EDIT - I should mention I have also tried sending an apikey in the request which I got from the service credentials section in the alchemy service bit off the dashboard but the same error occurs.
EDIt - The call to the API I am making looks like this:
POST https://alchemylanguage-nodejs-encekxdev-216.mybluemix.net/api/keywords
with headers:
text: 'this is some dummy text I have made'
I have also tried adding apikey to the headers.
Thanks.
You do not POST your application, you have to POST to the API.
Here is the link with API URL and various samples:
http://www.ibm.com/watson/developercloud/alchemy-language/api/v1
For example, here is a curl request for keywords:
curl -X POST \
-d "outputMode=json" \
-d "url=http://www.twitter.com/ibmwatson" \
"https://gateway-a.watsonplatform.net/calls/url/URLGetRankedKeywords?apikey=$API_KEY"
Make sure you export $API_KEYto your environment before running the command above. You can find the $API_KEY in the AlchemyAPI instance you created in Bluemix.

Does DocuSign Connect work with basic HTTP auth details?

I am using DocuSign connect to update the state of my app after an event happens on a document.
I have set up my account like so:
At the moment my "URL to Publish" looks something similar to https://key:secret#example.herokuapp.com. However when I look in the logs I always seem to receive something similar to:
error: Exception in EnvelopeIntegration.RunIntegration: key :: https://key:secret#example.herokuapp.com/webhook :: Error - The remote server returned an error: (401) Unauthorized
When I copy the Envelope Data into a file locally (complete-webhook.xml) and I run the following command through the command line it seems to run successfully:
curl -i -X POST -d #complete-webhook.xml https://key:secret#example.herokuapp.com/webhook
Has anybody got any ideas as to the reason why this could be happening?
When you use a url such as https://username:password#example.com/, your client takes the username:password part of the url and uses it to create an Authorization: Basic header.
You can try it yourself, create a requestb.in and then use the curl command
curl -X POST -d "fizz=buzz" http://username:password#requestb.in/12345
# where 12345 is your requestb.in address
The result on requestb.in:
A request to just /12345 (the incoming url does not include the username or password)
The request includes the header Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
If you put dXNlcm5hbWU6cGFzc3dvcmQ= through a base64 decoder, you get username:password
Answer, at this point, the Connect system does not support sending basic authentication information when it calls listeners. I have filed an internal feature request.
Work-around
Your listener url can include a query parameter that serves as a password. eg. `example.com/webhook/?pw=9e47a953-c105-44c5-ba5c-4bb77d63694d
Then, in your listener, simply reject any request that does not include the pw query parameter and the value that you chose.
In its requests to your listener, the Connect system will use any query parameters that you originally set when you added the Connect subscription.

How to show if a camera is online with the nest api

I'm working on a website that contains multiple Nest cameras and I wonder if it is possible to display if a camera is online before a user clicks on the link to view this cam.
I would do it with a XMLRequest in javascript but I cant find how to do it.
You would make a REST call for it, HTTP GET specifically. The request url would look like this:
https://developer-api.nest.com/devices/cameras/<device_id>/is_online?auth=<auth_token>
Using cURL it would look like this:
curl -v -L -H
"Authorization: Bearer <auth_token>"
-X GET "https://developer-api.nest.com/devices/cameras/<device_id>/is_online"
Reference: https://developer.nest.com/documentation/api-reference/overview#cameras

Resources