I'm building a Lambda in AWS, using python3, urllib3 and PoolManager.request()
My code:
def process_message(token, message):
try:
bucket_name = message['s3']['bucket']['name']
object_name = message['s3']['object']['key']
print(f"|---> Processing message {bucket_name}: {object_name}")
# build request json
req_json = { 'Bucket': bucket_name, 'Object': object_name }
http = urllib3.PoolManager()
bearer_token = "Bearer " + token
auth_hdr = {'Authorization': bearer_token }
ct_hdr = {'Content-Type': 'application/json'}
obj_request_headers = {**auth_hdr, **ct_hdr}
new_object_response = http.request('POST', oracle_notifier_listener_url, json = req_json, headers = obj_request_headers)
logger.debug("Got new_object response: %s", new_object_response)
except Exception as err:
print("*---> An error occurred processing new object notification")
raise err
I get the error:
{
"errorMessage": "HTTPResponse.__init__() got an unexpected keyword argument 'json'",
"errorType": "TypeError",
"requestId": "8d6c5511-6ca6-4245-8042-3aa5b3b98f89",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 23, in lambda_handler\n process_message(token, message)\n",
" File \"/var/task/lambda_function.py\", line 107, in process_message\n raise err\n",
" File \"/var/task/lambda_function.py\", line 102, in process_message\n new_object_response = http.request('POST', oracle_notifier_listener_url, json = req_json, headers = obj_request_headers)\n",
What's weird is, the request seems to go through just fine, I can see in the logs a 202 response status:
[DEBUG] 2024-10-15T17:05:11.150Z 8d6c5511-6ca6-4245-8042-3aa5b3b98f89 Starting new HTTPS connection (1): endpt.com:443
[DEBUG] 2024-10-15T17:05:12.375Z 8d6c5511-6ca6-4245-8042-3aa5b3b98f89 https://endpt:443 "POST /path/to/getObject HTTP/1.1" 202 None
I also tried:
new_object_response = http.request('POST', oracle_notifier_listener_url, json = json.dumps(req_json), headers = obj_request_headers)
I got the same response, same error.
FWIW, I'm using these docs: https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html
What am I doing wrong, oh great python devs worldwide?
This error indicates your urllib3 version is too old (1.x). The json
keyword argument was new in urllib3 v2.
Upgrade urllib3.
If you can't upgrade due to some other constraints, you may have to modify the code for 1.x support. In the case of json requests, you can replace:
http.request("POST", ..., json=req_json)
with
http.request("POST", ..., body=json.dumps(req_json).encode())