HTTP JSON API 请求响应“-1”
HTTP JSON API request responds "-1"
我正在尝试制作一个带有 ESP8266 模块的 Arduino 可以 GET
的 REST API,但是当我尝试向我的服务器发送请求时,我得到的 HTTP 代码是-1
,我无法在任何地方找到任何文档(它不在 HTTP 状态代码列表中 here)。
我从 -1
得到响应的 test-API 是 here, while a this API 工作正常。我的API很简单:
PHP API:
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
$array = array(
"status" => true
);
echo json_encode($array);
Arduino 代码 只是 ESP8266 示例之一,看起来像这样:
#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);
}
我可以使用 jQuery AJAX 从任何计算机本地向我的 API 发出 GET 请求,所以它一定是开放的?
是我的主机问题,还是我的文件丢失了一些 headers?
访问受 SSL 证书 (https) 保护的域时,您需要将 SSL 指纹指定为 http.begin
函数中的第二个参数:
String thumbprint = "the_thumbprint";
http.begin("https://api.site/api/get_details.json", thumbprint);
我正在尝试制作一个带有 ESP8266 模块的 Arduino 可以 GET
的 REST API,但是当我尝试向我的服务器发送请求时,我得到的 HTTP 代码是-1
,我无法在任何地方找到任何文档(它不在 HTTP 状态代码列表中 here)。
我从 -1
得到响应的 test-API 是 here, while a this API 工作正常。我的API很简单:
PHP API:
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
$array = array(
"status" => true
);
echo json_encode($array);
Arduino 代码 只是 ESP8266 示例之一,看起来像这样:
#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);
}
我可以使用 jQuery AJAX 从任何计算机本地向我的 API 发出 GET 请求,所以它一定是开放的?
是我的主机问题,还是我的文件丢失了一些 headers?
访问受 SSL 证书 (https) 保护的域时,您需要将 SSL 指纹指定为 http.begin
函数中的第二个参数:
String thumbprint = "the_thumbprint";
http.begin("https://api.site/api/get_details.json", thumbprint);