Http curl post request

Hi, is it possible to query the tibber api by http node?

example from docs:

curl \
-H "Authorization: Bearer 5K4MVS-OjfWhK_4yrjOlFe1F6kJXPVf7eQYggo8ebAE" \
-H "Content-Type: application/json" \
-X POST \
-d  '{ "query": "{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}" }' https://api.tibber.com/v1-beta/gql

from:
Tibber Developer

I want to use this:

subscription{
  liveMeasurement(homeId:"xxxxxx"){
    timestamp
    power
    accumulatedConsumption
    accumulatedCost
    currency
    minPower
    averagePower
    maxPower
  }
}

I don´t know how to put that in http request.

Inject -> http request

Inject node...

  • Take the JSON from the -D - add it to an inject node (set drop-down to JSON) that sets msg.payload

Http request...

  • Set URL to https://api.tibber.com/v1-beta/gql
  • Set the method to HEAD
  • Copy the 2 -H values to the headers
2 Likes

thank you Steve, but the json is invaild :thinking:

subscription{
  liveMeasurement(homeId:"xxx"){
    timestamp
    power
    accumulatedConsumption
    accumulatedCost
    currency
    minPower
    averagePower
    maxPower
  }
}

also this is invaild:

{ "query": "{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}

any idea?

I have an idea why the JSON is invalid (because it is)

But I have no idea what it is supposed to be.

If you provide a working cURL (with working data) I can tell you how to put it together.

Maybe you can take information from the tibber api contrib: node-red-contrib-tibber-api/nodes at master · bisand/node-red-contrib-tibber-api · GitHub

It is a graphql query.

also this is invaild:

The double quotes are not closed (?):

{ "query": "{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}"}
1 Like

Do I have to use this method:
https://cookbook.nodered.org/http/handle-query-parameters

No.

Did you check:

double quotes are not closed

I checked all I could, please try the example in my very first post.

Flow:

[{"id":"59b9af5caf250a13","type":"inject","z":"bc38480e05df57a2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":260,"wires":[["e9b6109e3d6e61db"]]},{"id":"c692182f3abaf363","type":"debug","z":"bc38480e05df57a2","name":"debug 349","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":260,"wires":[]},{"id":"8368f0c1781153d3","type":"http request","z":"bc38480e05df57a2","name":"","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":490,"y":260,"wires":[["c692182f3abaf363"]]},{"id":"e9b6109e3d6e61db","type":"function","z":"bc38480e05df57a2","name":"function 164","func":"const headers = {}\nconst token = \"5K4MVS-OjfWhK_4yrjOlFe1F6kJXPVf7eQYggo8ebAE\"\nheaders[\"Content-Type\"] = \"application/json\"\nheaders[\"Authorization\"] = `Bearer ${token}`\nmsg.method = \"POST\"\nmsg.headers = headers\nmsg.url = \"https://api.tibber.com/v1-beta/gql\"\nmsg.payload = { \"query\": \"{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}\" }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":260,"wires":[["8368f0c1781153d3"]]}]
1 Like

thank you, thats working.

Do you know how I could paste this query:

subscription{
  liveMeasurement(homeId:"xxx"){
    timestamp
    power
    accumulatedConsumption
    accumulatedCost
    currency
    minPower
    averagePower
    maxPower
  }
}

in your function ?

I tried:

const headers = {}
const token = "xy"
headers["Content-Type"] = "application/json"
headers["Authorization"] = `Bearer ${token}`
msg.method = "POST"
msg.headers = headers
msg.url = "https://api.tibber.com/v1-beta/gql"
msg.payload = { "subscription": "{liveMeasurement(homeId:"xxx") { timestamp power accumulatedConsumption accumulatedCost currency minPower averagePower maxPower }}" }
return msg;

but the function is always faulty :confounded:

The payload query needs to be a string
e.g.

msg.payload = {
    "query": `subscription{
  liveMeasurement(homeId: "xxx") {
        timestamp
        power
        accumulatedConsumption
        accumulatedCost
        currency
        minPower
        averagePower
        maxPower
    }
}` }

I used backticks, else all the string needs to be on one line and use single quotes. If you use double quotes the double quotes in the string need to be escaped \"

the function is valid... but the answere from tibber is null :confounded:

Actual response is "null" or the was no response?

Did you update the homeId value?

Did you return the msg at the end of the function?

Did you attach a debug node before going into the http node & check the exact value being sent?

Did you set all the headers?

1 Like

yes null

yes

yes

yes

yes

grafik

The developer documentation is quite explicit (and important to read).

liveMeasurement is a websocket feature.

Read the requirements.

  • first check if livemeasurements are actually enabled
  • if yes, subscribe with the post request
  • connect to the websocket - however, the websocket for tibber appears to have specific graphql requirements (see the Websocket subscription client requirements section) for the client, cannot be implemented with the standard node-red websocket nodes.

ok, that means it is not possible :thinking: :dotted_line_face:

Everything is possible but requires more effort.

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