I'm porting some esp8266 code to an esp32. Oddly enough this piece of the code works perfect on the 8266, but not on the 32. The server is erroring with "code 400, message Bad HTTP/0.9 request type ('POST')". Looking at wireshark i'm not really seeing any issues other than the server rejects with packets out of order. Any ideas? I have been looking at this for quite some time and don't see anything wrong.
code:
Code: Select all
char json_string[430];
serializeJson(dataToSend,json_string);
serializeJsonPretty(dataToSend,Serial);
if((WiFi.status() == WL_CONNECTED)){ //Check WiFi connection status
HTTPClient http;
http.begin("http://turnkey.serve.com:30030");
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(json_string);
String payload = http.getString(); //Get the response payload
http.end(); //Close connection move this up higher?
}
}
Code: Select all
{
"NetBios": "esp-micro-982261",
"Guid": "1401d38e2c-4bfcdad55583",
"ntpUTC": "02/28/2020 04:16:10",
"upHours": 0,
"upMinutes": 16,
"HeapB": 234980,
"MAC": "24:62:AB:D4:CE:88",
"IP": "192.168.43.37",
"AC": "PeriodicUpdate32",
"SW": "0.0.14"
}
Here's a python 2.7 server that recreates the error
Code: Select all
from BaseHTTPServer import BaseHTTPRequestHandler
import pprint
import json
localIPAddress = '192.168.1.80'
class PostHandler(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
json_data = json.loads(post_data)
pp = pprint.PrettyPrinter(indent=4)
print pp.pprint(json_data)
self.send_response(200)
self.end_headers()
return
if __name__ == '__main__':
from BaseHTTPServer import HTTPServer
server = HTTPServer((localIPAddress, 8080), PostHandler)
print 'Starting server, use <Ctrl-C> to stop'
server.serve_forever()
Here are the two tcp conversations between esp and server taken with wireshark on the server
ESP32 (server has errors):
Code: Select all
POST HTTP/1.1
Host: http://turnkey.serve.com:30030
User-Agent: ESP32HTTPClient
Connection: keep-alive
Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
Content-Type: application/json
Content-Length: 234
<head>
<title>Error response</title>
</head>
<body>
<h1>Error response</h1>
<p>Error code 400.
<p>Message: Bad HTTP/0.9 request type ('POST').
<p>Error code explanation: 400 = Bad request syntax or unsupported method.
</body>
{"NetBios":"esp-micro-982261","Guid":"1401d38e2c-4bfcdad55583","ntpUTC":"02/28/2020 04:09:47","upHours":0,"upMinutes":10,"HeapB":229956,"MAC":"24:62:AB:D4:CA:88","IP":"192.168.43.37","AC":"PeriodicUpdate32","SW":"0.0.14"}
Code: Select all
POST / HTTP/1.1
Host: http://turnkey.serve.com:30030
User-Agent: ESP8266HTTPClient
Connection: keep-alive
Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
Content-Type: application/json
Content-Length: 230
{"NetBios":"esp-micro-254565","Guid":"aafcd202f6-30a4504f734d","ntpUTC":"02/28/2020 04:13:53","upHours":0,"upMinutes":1,"HeapB":46400,"MAC":"4C:11:AE:0C:CA:FA","IP":"192.168.1.127","AC":"PeriodicUpdate","SW":"0.0.14"}HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.7.17
Date: Fri, 28 Feb 2020 04:13:53 GMT