2

Right now I am programming an API with python, and I have the following problem: I have to POST the following JSON to an url:

prescription = {
"Name": "file_name", # This is a string
"Body" : "xxx",# (File in base64 format)
"ParentId" : "xxxxxxxxxxxxxxxxxx", # This is a string
"ContentType" : "xxxxx/xxx" # This is a string
}

But when I try to do the following request:

requests.post(url, prescription)

I am getting the following error:

TypeError: Object of type bytes is not JSON serializable

How can I make a request for posting that JSON? Is it even possible?

Thanks for your help.

EDIT: Using

"Body": xxx.decode("utf-8")

worked, thanks for the help

2

1 Answer 1

1

You can do the following:

import base64
import requests

with open("your/file", "rb") as file:
    b64_file_content= base64.b64encode(file.read())

prescription = {
    "Name": "file_name",  # This is a string
    "Body": b64_file_content.decode("ascii"),  # (File in base64 format)
    "ParentId": "xxxxxxxxxxxxxxxxxx",  # This is a string
    "ContentType": "xxxxx/xxx",  # This is a string
}

requests.post("https://somewhere.world", prescription)
Sign up to request clarification or add additional context in comments.

Comments

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.