Rest http api post request

Hi All,

I'm looking to Log In to the Dashboard Developer REST HTTP API of a printer we have on site using Node Red to get the access token and ultimately be able to receive status information of the printer and forward it on to a PLC (Siemens)

Regarding the Log In to the API, the below instruction are given by the Printer supplier:
#######################################################################
Prerequisites
Make sure that you have the following prior to proceeding on with this section:

- Your AAAAAAAA.com username and password

- You have AAAAAAAA printer(s) connected to your AAAAAAAA.com account

- You have received the client ID and the client secret from AAAAAAAA

Login

Log in by sending an HTTP POST request to https://api.AAAAAAAA.com/developer/v1/o/token/ with

the following parameters in application/x-www-form-urlencoded content type:

● "grant_type": "password"

● "username": "your_AAAAAAAA_account_username"

● "password": "your_AAAAAAAA_account_password"

● "client_id": "your_client_id"

● "client_secret": "your_client_secret"

Example

The following is an example request of logging in using cURL command line tool. Please open

your Shell/Terminal and type the following:

curl --request POST \

--url https://api.AAAAAAAA.com/developer/v1/o/token/ \

--header 'content-type: application/x-www-form-urlencoded' \

--data

'grant_type=password&client_id=your_client_id&client_secret=yo

ur_client_secret&username=your_username&password=your_password

'

On Windows machines, you may need slightly different syntax like the following example:

rl -X POST https://api.AAAAAAAA.com/developer/v1/o/token \

-H "Content-Type: application/x-www-form-urlencoded" \

-d \

"grant_type=password&client_id=your_client_id&client_secret=yo

ur_client_secret&username=your_username&password=your_password

"

The API should return something like this:

{

"access_token": "your_access_token",

"token_type": "Bearer",

"expires_in": 86400,

"refresh_token": "your_refresh_token",

"scope": "developer-api"
}

###############################################################################
Iv created a very basic flow in NODERED to try and see if could connect and replicate the expected return:


and wrote function:

with HTTP Request Node:

Unfortunately it is not working for me and i don't have enough of an understanding of JSON or NODE RED, to know why. If any could give me some pointers, it would be much appreciated.

TIA

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

It helps us to make more informed answers as it means we can clearly see what you have done rather than try to work out what has happened from screenshots.

As the data is JSON (which is case sensitive), try using same case in your payload

image

1 Like

Simple as that, thanks very much for the follow up.

Hello,
Happy that you found some help.
What is the interest in you function node to initialize msg.header and msg.payload to empty object {} just before to give their correct definition?
Notice that for such code, a change node can do the same.

Pierre RUWET

1 Like

When I was doing a similar thing, my flow worked 100% once I added msg.header = {} to declare msg.headers as an object before defining . See this post here

The point @piwi447 is trying to make is that line 7 & line 8 both create a new object so line 7 is completely redundant.

In your situation, you were trying to set properties of an object that wasn't an object.

Thank you Steve, I have gone through that post again step by step and understand even more now!

At the time, I think I must have been so elated, I didn't fully grasp the implications of the change.

Hi Pierre,

Thanks for pointing this out, iv removed this from my Functions. Unfortunately I'm not proficient in JSON, but I'm learning a little bit through this application.

Thanks for the response.

My understanding is that you need to initialize the header and payload as empty object when you later want to define directly the keys one by one.
With for example: msg.payload.keynnnn = « value » where keynnn would be the key name and « value » the string affected to this key.

Pierre RUWET

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.