API PUT with JSON

Hi,

I want to work with API PUT but it's necessary to give a JSON frame with it. How can I do that?

try switching your http-request node's Method to PUT and whatever msg.payload you send to it will be the body of the request.

How can I send that body with it?

Which node does I need to send the body and where must I place it ?

Your question is how to create a msg.payload which will be the body of the request ?
to name a few .. you can use either an Inject, Change or Function node etc

What have you tried ? What is the JSON frame body of the request that you need to send ?

At this moment I have this script.
The JSON body is : json={'mode': 'NORMAL',"limit": {"unit": "PERCENTAGE", "value": 100 }}

[{"id":"55a991d4e79c0269","type":"inject","z":"b3be6624e0cc5c82","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":320,"wires":[["f4a8e4d3c14a6659","95460b32b28ead83","ed43323f17b1c448","03514bb389890642","9305a1779c424450","34016018192a8a7b"]]},{"id":"34016018192a8a7b","type":"http request","z":"b3be6624e0cc5c82","name":"Laadpaal sturen","method":"POST","ret":"obj","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/servicelocation/59792/actuator/1/on","tls":"","persist":false,"proxy":"","authType":"bearer","senderr":true,"x":460,"y":360,"wires":[["f905ccdb55bd2d80"]]},{"id":"f905ccdb55bd2d80","type":"debug","z":"b3be6624e0cc5c82","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":280,"wires":}]

Your Flow cannot be imported .. use the Preformatted Text icon when you share code
so it will be correctly formatted on the Forum.

image

Try this ..

[{"id":"55a991d4e79c0269","type":"inject","z":"54efb553244c241f","name":"inject data as Json","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"mode\":\"NORMAL\",\"limit\":{\"unit\":\"PERCENTAGE\",\"value\":100}}","payloadType":"json","x":330,"y":1760,"wires":[["34016018192a8a7b"]]},{"id":"34016018192a8a7b","type":"http request","z":"54efb553244c241f","name":"Laadpaal sturen","method":"POST","ret":"obj","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/servicelocation/59792/actuator/1/on","tls":"","persist":false,"proxy":"","authType":"bearer","senderr":true,"x":560,"y":1820,"wires":[["f905ccdb55bd2d80"]]},{"id":"f905ccdb55bd2d80","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":1820,"wires":[]},{"id":"ead31c6b5e54e5bb","type":"inject","z":"54efb553244c241f","name":"inject data as string","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"json={\"mode\": \"NORMAL\",\"limit\": {\"unit\": \"PERCENTAGE\", \"value\": 100 }}","payloadType":"str","x":330,"y":1880,"wires":[["34016018192a8a7b"]]},{"id":"377dc6d019125967","type":"comment","z":"54efb553244c241f","name":"OR","info":"","x":330,"y":1820,"wires":[]}]

(note that i changed the single quotes to double quotes for mode and NORMAL because single quotes are not valid Json)

[{"id":"f905ccdb55bd2d80","type":"debug","z":"b3be6624e0cc5c82","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":280,"wires":[]},{"id":"34016018192a8a7b","type":"http request","z":"b3be6624e0cc5c82","name":"Laadpaal sturen","method":"POST","ret":"obj","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/servicelocation/59792/actuator/1/on","tls":"","persist":false,"proxy":"","authType":"bearer","senderr":true,"x":460,"y":360,"wires":[["f905ccdb55bd2d80"]]},{"id":"55a991d4e79c0269","type":"inject","z":"b3be6624e0cc5c82","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":320,"wires":[["f4a8e4d3c14a6659","95460b32b28ead83","ed43323f17b1c448","03514bb389890642","9305a1779c424450","34016018192a8a7b"]]}]

better :wink:

i shared in my last post a Flow for you to try ..
( From the menu of the Node-red Editor you can select Import (or Ctrl-I) and copy paste to import and test )

By the way .. do you have any online documentation for your API ?

online documentation:

[type or paste code here](https://smappee.atlassian.net/wiki/spaces/DEVAPI/overview)

But on this moment I can't send a body with it. That's my problem, I don't know how to send the body (in json) with it

we understand the problem .. this is what we are trying to figure out :wink:
and i explained ways of sending a request body .. by sending your Json as part of the msg.payload

  1. you didnt tell as if you tried the flow example i sent you in my previous post ?
  2. from the documentation, which API method are you trying ? can you send us the exact link instead of the main page ? Is it the one with the Actuator ? Actuator ON

Yes I tried your example but I receive error 405 even if I try to send it as a string
the exact link: https://app1pub.smappee.net/dev/v3/chargingstations/6000000046/connectors/1/mode
json body: {"mode":"PAUSED"}

@UnborN I already found my problem, in the example was the method on 'Post' but it must 'Put'.

Thank you for the big help!

1 Like

its important to share with us the correct links to the documentation of the API
what we needed was this page : Set the charging mode of one of the connectors of a charging station

im glad it worked :+1:

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