Problem with POST GraphQL,

Hi
Trying to retrieve the energy prices from the energy company API via an ESP32 with the purpose to restrict heating during peak price hours.

I am able to communicate with the API via REQBIN and connect to the server through the ESP32 with the below code.

My problem is it returns:
-> Connected to server!
-> headers received
-> POST body missing, invalid Content-Type, or JSON object has no keys.

Any pointers?

#include <WiFiClientSecure.h>
#include <ArduinoJson.h>

const char*  server = "api.tibber.com";  // Server URL

const char* root_ca = \ 
"-----BEGIN CERTIFICATE-----\n" \ 
"MIIEkjCCA3qgAwIBAgITBn+USionzfP6wq4rAfkI7rnExjANBgkqhkiG9w0BAQsF\n" \
"ADCBmDELMAkGA1UEBhMCVVMxEDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNj\n" \
"b3R0c2RhbGUxJTAjBgNVBAoTHFN0YXJmaWVsZCBUZWNobm9sb2dpZXMsIEluYy4x\n" \
"OzA5BgNVBAMTMlN0YXJmaWVsZCBTZXJ2aWNlcyBSb290IENlcnRpZmljYXRlIEF1\n" \
"dGhvcml0eSAtIEcyMB4XDTE1MDUyNTEyMDAwMFoXDTM3MTIzMTAxMDAwMFowOTEL\n" \
"MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n" \
"b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n" \
"ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n" \
"9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n" \
"IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n" \
"VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n" \
"93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n" \
"jgSubJrIqg0CAwEAAaOCATEwggEtMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/\n" \
"BAQDAgGGMB0GA1UdDgQWBBSEGMyFNOy8DJSULghZnMeyEE4KCDAfBgNVHSMEGDAW\n" \
"gBScXwDfqgHXMCs4iKK4bUqc8hGRgzB4BggrBgEFBQcBAQRsMGowLgYIKwYBBQUH\n" \
"MAGGImh0dHA6Ly9vY3NwLnJvb3RnMi5hbWF6b250cnVzdC5jb20wOAYIKwYBBQUH\n" \
"MAKGLGh0dHA6Ly9jcnQucm9vdGcyLmFtYXpvbnRydXN0LmNvbS9yb290ZzIuY2Vy\n" \
"MD0GA1UdHwQ2MDQwMqAwoC6GLGh0dHA6Ly9jcmwucm9vdGcyLmFtYXpvbnRydXN0\n" \
"LmNvbS9yb290ZzIuY3JsMBEGA1UdIAQKMAgwBgYEVR0gADANBgkqhkiG9w0BAQsF\n" \
"AAOCAQEAYjdCXLwQtT6LLOkMm2xF4gcAevnFWAu5CIw+7bMlPLVvUOTNNWqnkzSW\n" \
"MiGpSESrnO09tKpzbeR/FoCJbM8oAxiDR3mjEH4wW6w7sGDgd9QIpuEdfF7Au/ma\n" \
"eyKdpwAJfqxGF4PcnCZXmTA5YpaP7dreqsXMGz7KQ2hsVxa81Q4gLv7/wmpdLqBK\n" \
"bRRYh5TmOTFffHPLkIhqhBGWJ6bt2YFGpn6jcgAKUj6DiAdjd4lpFw85hdKrCEVN\n" \
"0FE6/V1dN2RMfjCyVSRCnTawXZwXgWHxyvkQAiSr6w10kY17RSlQOYiypok1JR4U\n" \
"akcjMS9cmvqtmg5iUaQqqcT5NJ0hGA==\n" \
"-----END CERTIFICATE-----\n" ;

const char* ssid     = "xxxxxxxx";
const char* password = "yyyyyyyyy";


WiFiClientSecure client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  StaticJsonDocument<400> doc;
  delay(100);

  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    // wait 1 second for re-trying

    delay(1000);
  }

  Serial.print("Connected to ");
  Serial.println(ssid);

  client.setCACert(root_ca);
  
  doc = "{\"query\": \"{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}\" }";

  
  Serial.println("\nStarting connection to server...");
  if (!client.connect(server, 443)) {
    Serial.println("Connection failed!");
  }
  else {
    Serial.println("Connected to server!");
    client.println("POST /v1-beta/gql HTTP/1.1");
    client.println("Host: api.tibber.com");
    client.println("Authorization: Bearer actual-token-goes-here");
    client.println("Content-Type: applicatio/json");
    client.print("Content-Length: ");
    client.println(measureJson(doc));
    client.println();
    serializeJson(doc, client);
    client.println();
   

    while (client.connected()) {
      String line = client.readStringUntil('\n');
      if (line == "\r") {
        Serial.println("headers received");
        break;
      }
    }
    // if there are incoming bytes available
    // from the server, read them and print them:
    while (client.available()) {
      char c = client.read();
      Serial.write(c);
    }

    client.stop();
  }
}

void loop() {
  // do nothing
}

missing an 'n' here in application

try to print the line you receive in the while()

Should this not be:

doc["query"] = "...";

or

deserializeJson(doc, "{\"query\": \"{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}\" }");

Jesus... must have removed the "n" during the 50-ish attempts to get this working. New error now:
-> {"error":"syntax error"}

Replaced the doc with this and still
-> POST body missing, invalid Content-Type, or JSON object has no keys.

Thanks for the ideas! but still
-> POST body missing, invalid Content-Type, or JSON object has no keys.

-> Connected to server!
-> headers received
-> POST body missing, invalid Content-Type, or JSON object has no keys.HTTP/1.1 400 Bad Request
-> Server: awselb/2.0
-> Date: Sun, 18 Sep 2022 10:25:34 GMT
-> Content-Type: text/html
-> Content-Length: 122
-> Connection: close
image

Success!!!
this did the trick!
doc["query"] = "{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}";

by the way, on an ESP32 it's usually easier to leverage the HTTPClient class to make such requests. May be something like this (typed here from the top of my head)

  WiFiClientSecure client;
  HTTPClient http;

  client.setCACert(root_ca);

  http.begin(client, "https://api.tibber.com//v1-beta/gql" ); 

  // add necessary headers
  http.addHeader("Content-Type",   "application/json");
  http.addHeader("Authorization",  "Bearer actual-token-goes-here");

  // send POST request
  String payload = "{\"query\": \"{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}\" }";

  if (http.POST(payload) == HTTP_CODE_OK) {
    Serial.println(http.getString());
  }

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