# Rest http api post request

**URL:** <https://discourse.nodered.org/t/rest-http-api-post-request/73227>\
**Category:** General\
**Tags:** http-request, function-node\
**Created:** [5 January 2023 14:28 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227 "2023-01-05T14:28:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dedoyler](https://avatars.discourse-cdn.com/v4/letter/d/f0a364/32.png) [@Dedoyler](https://discourse.nodered.org/u/Dedoyler)\
**Post date:** [5 January 2023 14:28 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/1 "2023-01-05T14:28:29Z")

</div>

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](http://AAAAAAAA.com) username and password**

**- You have AAAAAAAA printer(s) connected to your [AAAAAAAA.com](http://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/](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:**

```auto
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:**

```auto
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:**

```auto
{

"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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/3/f3e1232a4204fdddabbdf7100796bed6466d547f.png)  
and wrote function:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b5dacbdbfbba4e046eb0e152090fca0a9cedb6b3.png)  
with HTTP Request Node:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f9d36a4bb925b97ae37e7b2359f8b55a1de8908.png)

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

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [5 January 2023 14:36 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/2 "2023-01-05T14:36:07Z")

</div>

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 `````)

````auto
``` 
   code goes here 
```

````

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

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 January 2023 15:19 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/3 "2023-01-05T15:19:12Z")

</div>

> [@Dedoyler](#):
>
> ```auto
> --data
> 
> 'grant_type=password&client_id=your_client_id&client_secret=yo
> 
> ur_client_secret&username=your_username&password=your_password
> 
> ```

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b7e4df7215df4dd976ae240233706b225fed5849.png)

---

<div class="post-metadata">

**Author:** ![Dedoyler](https://avatars.discourse-cdn.com/v4/letter/d/f0a364/32.png) [@Dedoyler](https://discourse.nodered.org/u/Dedoyler)\
**Post date:** [5 January 2023 15:31 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/4 "2023-01-05T15:31:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![piwi447](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/piwi447/32/67335_2.png) [@piwi447](https://discourse.nodered.org/u/piwi447)\
**Post date:** [5 January 2023 22:13 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/5 "2023-01-05T22:13:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [6 January 2023 09:01 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/6 "2023-01-06T09:01:16Z")

</div>

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](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/17)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 January 2023 09:45 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/7 "2023-01-06T09:45:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [6 January 2023 09:52 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/8 "2023-01-06T09:52:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dedoyler](https://avatars.discourse-cdn.com/v4/letter/d/f0a364/32.png) [@Dedoyler](https://discourse.nodered.org/u/Dedoyler)\
**Post date:** [6 January 2023 15:16 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/9 "2023-01-06T15:16:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![piwi447](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/piwi447/32/67335_2.png) [@piwi447](https://discourse.nodered.org/u/piwi447)\
**Post date:** [7 January 2023 08:53 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/10 "2023-01-07T08:53:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 January 2023 08:54 UTC](https://discourse.nodered.org/t/rest-http-api-post-request/73227/11 "2023-01-21T08:54:46Z")

</div>

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