使用urllib发送REST POST请求传递文件夹路径遇500错误如何解决?
Fixing the 500 Error in Your urllib GET Request
Let's walk through the issues in your code and fix them—no external libraries needed, just standard Python 3.6 urllib.
Key Issues Causing the 500 Error
- Wrong HTTP Method: Your original example request is a GET request (all parameters are in the URL query string), but your code sends a POST request. When you pass
datatourllib.request.Request, it automatically switches the method to POST, which the server likely doesn't expect for this endpoint—hence the 500 error. - Variable Name Mistakes:
- You used
self.loadRecordsRestCallbut didn't mention this code is in a class. If this is a standalone script,selfis invalid here. - You assigned the response object to
loadRecordsRestCall(the same name as your URL variable) and then tried to useloadRecordsResponse, which doesn't exist—this would throw aNameErroreven if the request worked.
- You used
- Unnecessary POST Data: For GET requests, parameters belong in the URL, not the request body.
Correct Implementation (GET Request)
This matches your original example URL exactly, using a GET request with parameters encoded in the query string:
from urllib.request import urlopen, URLError from urllib.parse import urlencode # Base endpoint URL load_records_url = 'http://localhost:8086/service/records/bulkimport' # Define your query parameters params = { 'overwrite': 'true', 'classroom': 'grade1', 'dir': 'C:\\Users\\temp\\classroomRecords\\grade1' } # Encode parameters and build the full URL encoded_params = urlencode(params) full_url = f"{load_records_url}?{encoded_params}" try: # Send the GET request and handle the response with urlopen(full_url) as response: print(f"Status: {response.getcode()}") # Decode the byte response to a readable string print(f"Response: {response.read().decode('utf-8')}") except URLError as e: print(f"Request failed: {e}") # If it's an HTTP error, print additional details if hasattr(e, 'code'): print(f"HTTP Error Code: {e.code}") if hasattr(e, 'reason'): print(f"Failure Reason: {e.reason}")
What This Does Differently
- Uses GET (matches your original request): Parameters are encoded and appended to the URL, just like your example link.
- Fixes variable names: No invalid
selfreferences, and the response object is properly named and used. - Adds proper error handling: Catches network errors and HTTP errors, giving you more context if something goes wrong.
- Decodes the response: Converts the raw byte output from
read()to a human-readable string.
If the Server Does Expect POST (Unlikely for Your Example)
If for some reason the endpoint requires POST requests (even though your example uses GET), fix the variable names and explicitly set the method:
from urllib.request import urlopen, URLError, Request from urllib.parse import urlencode load_records_url = 'http://localhost:8086/service/records/bulkimport' params = { 'overwrite': 'true', 'classroom': 'grade1', 'dir': 'C:\\Users\\temp\\classroomRecords\\grade1' } # Encode parameters for POST body data = urlencode(params).encode('ascii') # Explicitly set POST method (optional, since data triggers POST) req = Request(load_records_url, data=data, method='POST') try: with urlopen(req) as response: print(f"Status: {response.getcode()}") print(f"Response: {response.read().decode('utf-8')}") except URLError as e: print(f"Request failed: {e}") if hasattr(e, 'code'): print(f"HTTP Error Code: {e.code}") if hasattr(e, 'reason'): print(f"Failure Reason: {e.reason}")
But based on your original example URL, the GET version is the correct fix for your 500 error.
内容的提问来源于stack exchange,提问作者user9571515
相关产品推荐
相关产品推荐

