Skip to content

ESP8266HTTPClient GET/POST request returns -1 status code #5335

@8483

Description

@8483

The following code gets a server response of -1.

What am I doing wrong? I've done a bunch of research and this is the suggested code.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

void setup() {
    WiFi.begin("SSID", "password");
}

void loop() { 
    HTTPClient http;

    http.begin("https://jsonplaceholder.typicode.com/posts/1");
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    int httpCode = http.GET();
    String payload = http.getString();

    Serial.println(httpCode);           // -1
    Serial.println(payload);            // nothing

    http.end();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions