Arduino向Laravel API发出POST请求

Arduino向Laravel API发出POST请求,laravel,arduino,arduino-uno,Laravel,Arduino,Arduino Uno,嗨,几天来,我正试图开始与Arduino Uno Wifi rev2的简单项目的工作。其范围是从卡或芯片读取rfid数据,将rfid代码发送到Web服务器并从中读取响应。响应将包含分配给发送的rfid代码的用户名称。我试图通过从Arduino向laravel API发送POST请求来实现这一点,然后从laravel发送带有rfid用户名称的响应。 但我甚至并没有从读取rfid数据开始,因为我只能向Web服务器发送POST请求并从服务器读取响应 这是Arduino代码: #include <

嗨,几天来,我正试图开始与Arduino Uno Wifi rev2的简单项目的工作。其范围是从卡或芯片读取rfid数据,将rfid代码发送到Web服务器并从中读取响应。响应将包含分配给发送的rfid代码的用户名称。我试图通过从Arduino向laravel API发送POST请求来实现这一点,然后从laravel发送带有rfid用户名称的响应。 但我甚至并没有从读取rfid数据开始,因为我只能向Web服务器发送POST请求并从服务器读取响应

这是Arduino代码:

#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>

char ssid[] = "SSID_SECRET";
char pass[] = "PASS_SECRET";
int port = 80;
const char serverAddress[] = "https://www.schedy.sk";  // server name

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);

int status = WL_IDLE_STATUS;

void setup() {
  Serial.begin(9600);
   while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass); 
    delay(5000);
  }
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  IPAddress gateway = WiFi.gatewayIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
  Serial.println("making POST request");
  String postData = "rfid=abcde&test=12";
  Serial.print("Post Data Length: ");
  Serial.println(postData.length());

  client.beginRequest();
  client.post("/api/rfids");
  client.sendHeader("Content-Type", "application/x-www-form-urlencoded");
  client.sendHeader("Content-Length", postData.length());
  //client.sendHeader("X-Custom-Header", "custom-header-value");
  client.beginBody();
  client.print(postData);
  client.endRequest();

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait five seconds");
  delay(5000);
}
在RfidController.php中:

public function index()
{
   return "get test";
}

public function store(Request $request)
{
   return response("post test")
}
我已尝试使用url:发布和获取请求。一切看起来都很好,但有了Arduino,我的状态代码仍然是400:

making POST request
Post Data Length: 17
Status code: 400
Response: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at kmbtwo.vps.websupport.sk Port 80</address>
</body></html>

Wait five seconds
发出POST请求
发布数据长度:17
状态代码:400
答复:
400错误请求
错误的请求
您的浏览器发送了此服务器无法理解的请求。


Apache/2.4.18(Ubuntu)服务器,位于kmbtwo.vps.websupport.sk端口80 等五秒钟

我对这个问题感到非常绝望。我尝试了几个库和过程,但仍然得到400个状态代码或-3。在server-apache2日志中,我无法获得有关该问题的更多信息。。。只是有人试图向/api/rfid发出POST请求,但答案是400。我不知道原因。有人能帮我吗

问题在于库-ArduinoHttpClient.h。使用此库无法发出https请求。我已经用wifinina.h解决了这个问题,我从服务器上得到了成功的答案。

Hello@Johny,只是为了检查是否正常,当您添加
auth:api
中间件时,您可能需要一个令牌来传递您的请求。另外,schedy.sk是一个活动域吗?您确定不应该在那里使用测试URL吗?如果你还没有找到邮递员,我建议你去看看,这会让你更容易。此外,请检查storage/logs/laravel.log,看看是否有任何线索。Hi@plushyObject,请确保我已对auth:api路由进行了注释,但没有任何更改。仍然有400个错误。看起来apache服务器甚至在到达laravel api之前就会停止post请求,因此laravel日志中没有任何内容。URL正确吗?您的错误显示kmbtwo.vps.websupport.sk,但您的代码显示服务器地址,因为URL应该是正确的。错误显示kmbtwo。。。因为它只是多个域所在服务器的名称。我也尝试过GETrequest,但结果仍然是相同的-400状态代码。还尝试将端口号更改为443(https)并将https更改为http,但没有任何积极的更改。当我用相同的url测试GET和POST请求时,一切都很好——服务器上有200个状态代码和响应体。我对Arduino不是很在行。是否有一种方法可以检查请求/响应头,以便将它们与reqbin进行比较?
making POST request
Post Data Length: 17
Status code: 400
Response: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at kmbtwo.vps.websupport.sk Port 80</address>
</body></html>

Wait five seconds