简体   繁体   English

如何在 Arduino IDE 中向 http 请求添加 Headers

[英]How to add Headers to http request in Arduino IDE

This is the code in Arduino IDE that does not work (httpCode = -1):这是 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);
}

And here is the "exact" same code in python that works:这是 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)

I am veeeery new to programming in Arduino IDE, I only now python to be fair so my guess is that there are probably couple of mistakes in my code.我是 Arduino IDE 的编程新手,公平地说,我现在才 python 所以我的猜测是我的代码中可能有几个错误。 Any ideas how to fix it?任何想法如何解决它?

The arduino library I'm using doesn't send the additional headers on a GET unless it's wrapped in beginRequest() and endRequest()我正在使用的 arduino 库不会在 GET 上发送额外的标头,除非它被包装在 beginRequest() 和 endRequest() 中

http.beginRequest();
http.get(request->url);
http.sendHeader("x-rapidapi-key:" + request->apiKey);;
http.endRequest();

I was stuck on this way too long today.我今天被困在这条路上太久了。

Your use of addHeader looks fine but it looks like you are trying to access a site over HTTPS/TLS.您对addHeader的使用看起来不错,但看起来您正在尝试通过 HTTPS/TLS 访问站点。

The Arduino HTTP client needs a fingerprint/hash of the certificate that is used by the server. Arduino HTTP 客户端需要服务器使用的证书的指纹/哈希。 This is used in place of the usual validation of the certificate due to the CPU and memory constraints of an Arduino device.由于 Arduino 设备的 CPU 和 memory 约束,这用于代替通常的证书验证。

You need to configure the certificate thumbprint for the get to work.您需要配置证书指纹get开始工作。 eg define the fingerprint for the server's certificate例如定义服务器证书的指纹

const uint8_t fingerprint[20] = { 0xe4, 0x5b, 0x13, 0x58, 0xc0, 0x4c, 0x27, 0xcb, 0x84, 0x4e,
                                  0xe1, 0xbb, 0x28, 0xf5, 0x36, 0x7c, 0x4f, 0x06, 0xa0, 0x1a };

Then supply this in the call to begin()然后在对begin()的调用中提供这个

    http.begin("https://api-football-v1.p.rapidapi.com/v2/fixtures/team/33/next/10",
               fingerprint);

I have not subscribed to the API on this platform so I get a valid 403 response when I try this:我没有在这个平台上订阅 API 所以当我尝试这个时我得到一个有效的 403 响应:

09:23:13.550 -> 403
09:23:13.550 -> {"message":"You are not subscribed to this API."}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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