0

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"```

  ]
} ```



1
  • Is the base64.standard_b64encode(b"token:" + TOKEN)}? Seems like it could be because its bytes. The header values should be a string and not bytes Commented Apr 27, 2021 at 2:02

1 Answer 1

1

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")}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I made the whole header a string and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.