python - 如何在 Arduino IDE 中向 http 请求添加标头
问题描述
这是 Arduino IDE 中不起作用的代码(httpCode = -1):
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
char ssid[] = "-------";
const char* password = "------";
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting...");
}
}
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
HTTPClient http; //Object of class HTTPClient
http.begin("https://api-football-v1.p.rapidapi.com/v2/fixtures/team/33/next/10");
http.addHeader("x-rapidapi-key", "-------");
int httpCode = http.GET();
Serial.println(httpCode);
if (httpCode > 0)
{
Serial.println(http.getString());
}
http.end(); //Close connection
}
delay(60000);
}
这是python中“完全相同”的代码:
import requests
url = "https://api-football-v1.p.rapidapi.com/v2/fixtures/team/33/next/10"
headers = {
'x-rapidapi-key': "--------",
}
response = requests.request("GET", url, headers=headers)
print(response.text)
我对在 Arduino IDE 中编程非常陌生,我现在才公平地说 python,所以我的猜测是我的代码中可能有几个错误。任何想法如何解决它?
解决方案
我正在使用的 arduino 库不会在 GET 上发送附加标头,除非它包含在 beginRequest() 和 endRequest() 中
http.beginRequest();
http.get(request->url);
http.sendHeader("x-rapidapi-key:" + request->apiKey);;
http.endRequest();
我今天被困在这条路上太久了。
推荐阅读
- scala - 在 Spark 中将可选参数建模为 UDF 的最佳方法是什么?
- laravel - 语法错误,我的 laravel 代码中的文件意外结束
- javascript - 将巨大数组中的每个 JavaScript 对象与另一个对象合并
- python - 如何打印2条水平线
- angular - npm WARN deprecated core-js@2.6.11: core-js@<3 不再维护,由于问题数量不推荐使用
- windows - 卸载后在 Windows 功能中看不到 Internet explore
- reactjs - Reactjs TypeError:无法获取未定义或空引用的属性“字符串”
- scala - 如何正确进行涉及异步方法的错误处理
- php - 我可以在脚本中将 PHP 函数设为“私有”吗?
- c# - 访问 List 中的元素
在 C# 中使用十进制值