Chat freely about anything...

User avatar
By rocketchef
#90035 I'm trying to access an API (https://api.coronavirus.data.gov.uk) on my Wemos D1 mini, and do something with the JSON data.

Problem: the HTTPclient is getting back compressed page content (e.g. "^_<8b>^H^@.Hï_^BÿíÙÁjÃ0^L^Fàw"). I think I've now identified this as gzip compressed content, by trying it in cURL with the --compressed flag - this sucessfully decodes the content.

The strange part that I can't figure out, is that with other APIs (e.g. github) - the content is gzip compressed according to the headers, but is returned decompressed on the D1 mini, so there's no issue reading it.

Can anyone help here?

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>

 const char* host = "https://api.coronavirus.data.gov.uk";
// const char* host = "https://api.github.com";

int httpsPort = 443;

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

void setup(void){
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

  WiFiClientSecure client;
  client.setInsecure(); //the magic line, use with caution
  client.connect(host, httpsPort);

  HTTPClient https; 

    Serial.print("[HTTPS] begin...\n");
    if (https.begin(client, "https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=nation;areaName=england&structure=%7B%22name%22:%22areaName%22%7D")) {  // HTTPS
//    if (https.begin(client, "https://api.github.com")) {  // HTTPS

      Serial.print("[HTTPS] GET...\n");
      // start connection and send HTTP header
      int httpCode = https.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
          Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    } else {
      Serial.printf("[HTTPS] Unable to connect\n");
    }


  Serial.println("Wait 20s before next round...");
  delay(200000);