Qt发送PUT请求报Bad request type PUT,curl请求正常
Hey there! Let's figure out why your PUT request is acting up while GET and POST work smoothly. Since the other methods are functioning, the problem is almost certainly tied to specific details in your do_PUT implementation that differ from your working GET/POST handlers. Let's walk through the most common culprits:
1. You're not reading the PUT request body correctly
Unlike some frameworks, BaseHTTPRequestHandler doesn't automatically parse the request body for PUT requests. If you forgot to read the incoming data (or didn't handle the Content-Length header properly), your server might hang, fail to process the request, or return an unexpected response.
Add this code at the start of your do_PUT method to properly fetch the request body:
def do_PUT(self): """ Processes PUT request. It can be test...""" # Get content length from headers content_length = int(self.headers.get('Content-Length', 0)) # Read the request body request_body = self.rfile.read(content_length) # Your existing processing logic here
Compare this to how you handle POST requests—chances are you already do this for POST but missed it for PUT.
2. Your response headers are incomplete or missing
PUT requests require a proper HTTP response just like GET/POST. If you skipped critical steps like sending a status code, setting content-type headers, or calling end_headers(), the Qt client might not recognize the response as valid.
Make sure your response follows this structure:
def do_PUT(self): # ... (request body reading and processing) # Send success status code (200 for OK, 201 for created) self.send_response(200) # Set appropriate content type (match what your Qt client expects) self.send_header('Content-Type', 'application/xml') # End headers to signal start of response body self.end_headers() # Write your XML response (adjust as needed) self.wfile.write(b'<response>PUT request processed successfully</response>')
Double-check that you're doing all these steps in your GET/POST handlers and mirror them in PUT.
3. Request parsing logic differs from POST
If your PUT request carries XML data (like your GET response), make sure you're parsing it the same way you do for POST. For example, if you use xml.etree.ElementTree to parse POST bodies, ensure you're applying the same logic to the PUT request body you read. A mismatch here could lead to silent failures or invalid responses.
4. Test the server directly (bypass Qt)
Rule out whether the issue is server-side or client-side by testing your PUT endpoint with a tool like curl:
curl -X PUT -H "Content-Type: application/xml" -d "<test_data>sample</test_data>" http://your-server-ip:port/your-path
If this curl request works as expected, the problem is likely in how your Qt app constructs the PUT request (e.g., missing headers, incomplete body, or incorrect URL). If curl fails too, the issue is definitely in your do_PUT implementation.
5. Add debug logging to diagnose what's happening
Insert detailed logs into your do_PUT method to see exactly what the server is receiving:
def do_PUT(self): """ Processes PUT request. It can be test...""" print("=== Received PUT Request ===") print(f"Request Headers: {self.headers}") content_length = int(self.headers.get('Content-Length', 0)) request_body = self.rfile.read(content_length) print(f"Request Body: {request_body.decode('utf-8')}") # Your processing logic here print("=== Finished Processing PUT Request ===")
This will show you if the request headers are correct, if the body is being received fully, and where your code might be deviating from expected behavior.
Start with these checks—most PUT issues with BaseHTTPRequestHandler boil down to missing request body reading or incomplete responses.
内容的提问来源于stack exchange,提问作者rbaleksandar

