简体   繁体   English

如何解决400错误的请求

[英]How do I troubleshoot a 400 Bad Request

I am running a program that reports data about a pump runtime and amps to a server using a POST request to a PHP page. 我正在运行一个程序,该程序使用对PHP页面的POST请求向服务器报告有关泵运行时间和安培的数据。 The system is running fine on several Arduino Pro Minis. 该系统在多个Arduino Pro Mini上运行良好。 I am testing it out on a board that I built using a 328 and Arduino Uno bootloader, and of the 4 different POST requests, two work and two fail. 我正在使用328和Arduino Uno引导程序构建的板上进行测试,并且在4个不同的POST请求中,两个工作失败,两个失败。

I believe this to be caused by the Arduino poorly forming the POST request, but I have no way to verify this since the Arduino Serial.prints the correct string. 我相信这是由于Arduino的POST请求格式不正确引起的,但是由于Arduino Serial.prints正确的字符串,因此我无法进行验证。 If I manually hard code the string, it also works. 如果我手动对字符串进行硬编码,它也可以工作。

I form the data this way: 我以这种方式形成数据:

byte runtime = analogRead(A3); // generates random data
float amps = analogRead(A2) / 100;
char cruntime[4]; // create char array for runtime
char camps[5];
dtostrf(runtime, 4, 0, cruntime);
dtostrf(amps, 2, 1, camps);
String sruntime = String(cruntime);
sruntime.trim();
String samps = String(camps);
samps.trim();
String cwpdata = "serial=test&runtime=" + sruntime + "&amps=" + samps; // posted to server

I've check my server for errors, but couldn't find records of these. 我已经检查我的服务器是否有错误,但是找不到这些记录。 Is there anyway I can see what is being posted each time so I can see where the request is not forming properly? 无论如何,我每次都能看到正在发布的内容,因此我可以看到请求的格式不正确吗?

Here is the code in the Arduino to post data: 这是Arduino中用于发布数据的代码:

void postData(String inputData, String filename) {
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("POST /dev/telemetry/test/" + filename + ".php HTTP/1.1");
    client.println("Host: www.SERVER.com"); // changed for privacy
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: "); 
    client.println(inputData.length()); 
    Serial.println(inputData);
    Serial.print("Data Length: ");
    Serial.println(inputData.length());
    client.println("Connection: close");
    client.println(); 
    client.println(inputData);
    client.println();
  } // close if client

  wdt_reset();
  unsigned long startMillis = millis();
  unsigned long endMillis = startMillis + timeout;
  while(client.connected() && (millis() < endMillis)) {
    wdt_reset();
    while(client.available()) {
      char ch = client.read();
      Serial.print(ch);
      wdt_reset();
    } 
  }
  // if the server's disconnected, stop the client:
  Serial.println();
  client.stop();
  int memory = freeMemory();
  if (memory < 125) {
    delay(15000);
  }
}

I would hazard a guess that, sometimes, your inputData contains unescaped characters that are not properly encoded before they are sent in the POST body. 我可能会猜测,有时候,您的inputData包含未转义的字符,这些字符在POST正文中发送之前未正确编码。

Within "serial=test&runtime=" + sruntime + "&amps=" + samps , if either sruntime or samps contains unescaped characters, it could very well throw the server-side parsing off. "serial=test&runtime=" + sruntime + "&amps=" + samps ,如果sruntimesamps包含未转义的字符,则很可能会抛出服务器端的解析。 Are there any "unprintable" control bytes hiding in the Serial.println(inputData) that might have been overlooked? 是否有任何“不可打印的”控制字节隐藏在Serial.println(inputData) ,可能被忽略了?

"Safe" characters, by the way, are AZ, az, 0-9, dash, underscore, dot, tilde, and the plus sign (which means space). 顺便说一下,“安全”字符是AZ,az,0-9,破折号,下划线,点,代字号和加号(表示空格)。 Everything else would have to be percent-encoded before being sent. 其他所有内容在发送之前都必须进行百分比编码 It gets a little hairier if the data happens to be Unicode. 如果数据碰巧是Unicode,它将变得更加毛茸茸

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

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