SEnd text file via AP Pico network with nice formatted response.

 Send text file via AP Pico network with nice formatted response.


From perplexity I got this code to format responses nicely:(Worked. See screenshot of Thonny.

def parse_request(request): # Decode the bytes to a string request_str = request.decode('utf-8') # Split the request into lines lines = request_str.split('\r\n') # Parse the first line (request line) method, path, protocol = lines[0].split(' ') # Parse headers headers = {} for line in lines[1:]: if ':' in line: key, value = line.split(':', 1) headers[key.strip()] = value.strip() # Parse query parameters query_params = {} if '?' in path: path, query_string = path.split('?', 1) params = query_string.split('&') for param in params: if '=' in param: key, value = param.split('=') query_params[key] = value # Format the parsed data formatted_request = f"Method: {method}\n" formatted_request += f"Path: {path}\n" formatted_request += f"Protocol: {protocol}\n" formatted_request += "Headers:\n" for key, value in headers.items(): formatted_request += f" {key}: {value}\n" if query_params: formatted_request += "Query Parameters:\n" for key, value in query_params.items(): formatted_request += f" {key}: {value}\n" return formatted_request # In your main loop, replace the print statement with: print('Content received by Pico:') print(parse_request(request))

Comments

Popular posts from this blog

Reading LittleFS file into buffer for sending

ESP32 buttons and bouncing