Reputation: 363
I have the code below in a AWS lambda function which makes a REST post request. The code worked in 2.7, but is throwing an error in 3.7.
The error is in the header portion of the request. But I'm not clear on how to fix it.
The code snippet is listed below:
DOMAIN = 'xxx-yyy.com'
TOKEN = 'xdfbvgsded5e9fb99a'
JOB = 1
build_url = f'https://{DOMAIN}/api/2.0/jobs/run-now'
response = requests.post(build_url,headers={'Authorization': b"Basic " + base64.standard_b64encode(b"token:" + TOKEN)}, json={"job_id": JOB})
# response = requests.get("http://www.google.com")
if response.status_code == 200:
print("Request Submitted")
else:
print("Error launching cluster")```
Error message
```{
"errorMessage": "can't concat str to bytes",
"errorType": "TypeError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n headers = {'Authorization': b\"Basic \" + base64.standard_b64encode(b\"token:\" + TOKEN)}\n"```
]
} ```
Upvotes: 0
Views: 799
Reputation: 559
You are trying to concat an str
(TOKEN
) to a bytes
var.
To fix make TOKEN
a bytes
variable:
TOKEN = b'xdfbvgsded5e9fb99a'
authorization = b"Basic " + base64.standard_b64encode(b"token:" + TOKEN)
I think the Authorization
header needs an str
, but because you have a bytes, here are the two options.
as bytes:
headers={'Authorization': authorization}
as str:
headers={'Authorization': authorization.decode("utf-8")}
Upvotes: 1