【问题标题】:Arduino POST request to Laravel APIArduino POST 对 Laravel API 的请求
【发布时间】:2020-01-20 20:24:02
【问题描述】:

您好几天,我正在尝试使用 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。不知道原因。有人可以帮助我吗?

【问题讨论】:

  • 您好@Johny,只是为了进行完整性检查,当您添加auth:api 中间件时,您可能需要一个令牌才能通过您的请求。另外,schedy.sk 是实时域吗?你确定你不应该在那里使用测试 URL 吗?如果您还没有,我建议您查看 Postman,它会让您更轻松。另外,查看 storage/logs/laravel.log 看看有没有线索。
  • 嗨@plushyObject,我确定我已经评论了 auth:api 路线,但没有任何改变。仍然收到 400 错误。看起来 apache 服务器甚至会在到达 laravel api 之前停止发布请求,所以 laravel 日志中没有任何内容。
  • 网址是否正确?您的错误显示 kmbtwo.vps.websupport.sk 但您的代码显示服务器地址为schedy.sk
  • 网址应该没问题。错误显示 kmbtwo ... 因为它只是多个域的服务器名称。我也尝试过 GETrequest 但结果仍然相同 - 400 状态码。还尝试将端口号更改为 443 (https) 并将 https 更改为 http,但没有任何积极的变化。当我使用具有相同 URL 的 reqbin.com 测试 GET 和 POST 请求时 - schedy.sk/api/rfids 一切正常 - 来自服务器的 200 个状态代码和响应正文。
  • 有趣。我不太擅长 Arduino。有没有一种方法可以检查请求/响应标头以将它们与 reqbin 进行比较?

标签: laravel arduino arduino-uno


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多