'ESP8266, how to make post request with graphQL

I am very new to programming with ESP8266 and can't get this to work. I am successfully connected to the internet. With the <ESP8266HTTPClient.h> I am writing the following code:

void loop() {
 
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
 
    HTTPClient http;    //Declare object of class HTTPClient
 
    http.begin("https://api.entur.io/journey-planner/v2/graphql");      //Specify request destination
    http.addHeader("Content-Type", "application/json");  //Specify content-type header
    http.addHeader("ET-Client-Name", "student-bussAPI"); //Identification requested by the API

    int httpCode = http.POST("{\"query\":\"{authorities{id}}\"}");   //Send the request
    String payload = http.getString();                  //Get the response payload
 
    Serial.println(httpCode);   //Print HTTP return code
    Serial.println(payload);    //Print request response payload
 
    http.end();  //Close connection
 
  } else {
 
    Serial.println("Error in WiFi connection");
 
  }
 
  delay(30000);  //Send a request every 30 seconds
 
}

the code runs with serial output -1 I have tested the query request on postman, and it worked fine there.



Solution 1:[1]

The "-1" you are getting is caused by the attempt to establish a connection to a strictly HTTPS service via HTTP. As the code examples in the ESP8266HttpClient library explain, "httpCode will be negative on error" - what you are seeing is the printing of a http status code of -1.

The easiest way to achieve what you are trying to do here (Query a HTTPS API via the ESP8266httpClient library) would be to connect to it using the current SHA-1 fingerprint of the certificate used by the service as a second parameter of your call to "http.begin".

Example to answer your specific question:

 
  HTTPClient http;    //Declare object of class HTTPClient

  const char* host = "https://api.entur.io/journey-planner/v2/graphql";
  const char* fingerprint ="Current sha-1 fingerprint goes here";
   
  http.begin(host, fingerprint);     //Specify request destination and fingerprint for cert to make it do HTTPS
  http.addHeader("Content-Type", "application/graphql"); //Content type here is important; Its not JSON
  http.addHeader("ET-Client-Name", "Esp8266-BitBrb");

I should also mention that the specific API you are using here has a strict check on content type, so you should change your header to match the data type the API accepts, "application/graphql", or else you'll have trouble :)

Incidentally, i have the full code from a project i did last year with the same service you are using (Hi, fellow Norwegian and EnTur user) available here: https://www.bitbrb.com/electronics/graphql-querying-a-bus

I see they got a new certificate earlier this year, other than that you should be able to cut and paste.

good luck :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gr00t