gpt4 book ai didi

laravel - Arduino POST 请求到 Laravel API

转载 作者:行者123 更新时间:2023-12-05 07:14:39 24 4
gpt4 key购买 nike

您好,这几天我正在尝试使用 Arduino Uno Wifi rev2 开始一个简单的项目。范围是从卡或芯片读取 rfid 数据,将 rfid 代码发送到网络服务器并从中读取响应。响应将包含分配给发送的 rfid 代码的用户名。我试图通过从 Arduino 向 laravel API 发送 POST 请求来实现这一点——之后从 laravel 将发送带有 rfid 用户名称的响应。但我什至没有开始读取 rfid 数据,因为我一直坚持向网络服务器发送 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);
}

Laravel API。仅出于测试目的,我在/routes/api.php 中定义了简单的路由:

Route::middleware('auth:api')->get('/user', function (Request $request)) {
return $request->user();
});

Route::get('/rfids', 'RfidController@index');
Route::post('/rfids', 'RfidController@store');

在 RfidController.php 中是:

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

public function store(Request $request)
{
return response("post test")
}

我已经尝试使用 https://reqbin.com/ 发布和获取请求对于网址:https://www.schedy.sk/api/rfids .一切看起来都很好,但使用 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

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

最佳答案

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

关于laravel - Arduino POST 请求到 Laravel API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59830384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com