简体   繁体   中英

HTTP JSON API request responds “-1”

I'm trying to make a REST API that an Arduino with an ESP8266 module can GET , but when I try to send a request to my server the HTTP Code that I get is -1 , which I have not been able to find any documentation on anywhere (it's not in the list of HTTP status codes here ).

My test-API that I get the -1 response from is here , while a this API works fine. My API is simple:

PHP API:

header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *'); 

$array = array(
    "status" => true
);

echo json_encode($array);

The Arduino code is just one of the ESP8266 examples, and looks like this:

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

const char* ssid = "Next-Guest";
const char* password = "";

void setup () {

  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);
    Serial.print("Connecting..");

  }

}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    //http.begin("http://jsonplaceholder.typicode.com/users/1"); <- this works
    http.begin("https://makerspace.albe.pw/api/getDoorStatus.php"); // <- this doesn't
    int httpCode = http.GET();

    Serial.println(httpCode);
    if (httpCode > 0) {
      String payload = http.getString();
      Serial.println(payload);
    }
    http.end();
  }
  delay(30000);
}

I can do a GET request locally from any computer to my API using jQuery AJAX, so it must be somewhat open?

Is my host the problem, or are my files missing some headers?

When accessing a domain protected with a SSL certificate (https), you need to specify the SSL thumbprint as a second parameter in the http.begin function:

String thumbprint = "the_thumbprint";
http.begin("https://api.site/api/get_details.json", thumbprint);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM