ESP-AT fails with HTTP POST

Piotrulo1
Posts: 3
Joined: Wed Nov 08, 2023 11:31 am

ESP-AT fails with HTTP POST

Postby Piotrulo1 » Wed Nov 08, 2023 11:43 am

Hi guys, I work on http client using esp-at on my esp32-wroom-32u, i encountered problem, when i connect to the server and send this command:

Code: Select all

AT+HTTPCLIENT=3,1,"http://192.168.31.31:8080/","","",1,"key=value"
simple python server created with command

Code: Select all

python3 -m http.server -b 192.168.31.31 8080
i get response as expected:

Code: Select all

AT+HTTPCLIENT=3,1,"http://192.168.31.31:8080/","","",1,"key=value"
+HTTPCLIENT:497,<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""
"http://www.w                   3.org/TR/html4/strict.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" con                   tent="text/html;charset=utf-8">
      <title>Error response</title>
   </head>
   <bo                   dy>
   <h1>Error response</h1>
   <p>Error code: 501</p>
   <p>Message: Un                   supported method ('POST').</p>
   <p>Error code explanation: HTTPStatus.NOT_IMPLEM                   ENTED - Server does not support this operation.</p>
   </body>
</html>



ERROR
but when i do the same with this server:

Code: Select all

#!/usr/bin/env python3
"""
License: MIT License
Copyright (c) 2023 Miel Donkers

Very simple HTTP server in python for logging requests
Usage::
    ./server.py [<port>]
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging

class S(BaseHTTPRequestHandler):
    def _set_response(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
        self._set_response()
        self.wfile.write("<html><body><h1>Get Request Received!</h1></body></html>".format(self.path).encode('utf-8'))

    def do_POST(self):
        print("post!!!")
        content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
        post_data = self.rfile.read(content_length) # <--- Gets the data itself
        logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
                str(self.path), str(self.headers), post_data.decode('utf-8'))

        self._set_response()
        self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))

def run(server_class=HTTPServer, handler_class=S, port=8081):
    logging.basicConfig(level=logging.INFO)
    server_address = ('192.168.31.31', port)
    httpd = server_class(server_address, handler_class)
    logging.info('Starting httpd...\n')
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    logging.info('Stopping httpd...\n')

if __name__ == '__main__':
    from sys import argv

    if len(argv) == 2:
        run(port=int(argv[1]))
    else:
        run()
there is only ERROR

Code: Select all

AT+HTTPCLIENT=3,3,"http://192.168.31.31:8080","192.168.31.31:8080","/",1,"hi"
ERROR
When i tried to test server with curl -X POST http://192.168.31.31:8080 -d "key=value" server answered.

Did somebody encounter something similar?

Who is online

Users browsing this forum: No registered users and 43 guests