Home > Mobile >  Why is my post request with Python's requests library filled with "lorem ipsem" text?
Why is my post request with Python's requests library filled with "lorem ipsem" text?

Time:06-10

I am working on a larger script, and ran into a weird issue when trying to post a file along with a json token.

I have successfully used the requests.post() function to just send JSON data to the server. Howerver, when I tried to send a file and JSON data, the post fails, and when I examin the post's body, it's full of "lorem ipsum" text that I didn't put there, which causes the request to fial, AND my JSON data is no where to be found.

import requests

SERVER = "http://localhost:3030/"

test_token = {'session_uuid': '90f50db9-39b1-476c-9fa4-3b7d2af51ad4',
    'client_uuid': '371f291c-2a65-4384-9e01-bf3ee880b777',
    'expiration': 'Wed Jun 18 19:20:21 EDT 2022'}

def post_upload_endpoint(endpoint, path, payload):
    try:
        print("Payload: ", payload) #just to make sure it's what I expect
        r = requests.post(endpoint, files=path, json=payload)
        print("Request body: ", r.request.body)
        return r.json()
    except Exception as x:
        print("Exception: ", x)
        return False, "Failed: {}".format(endpoint)


def main():
    path = {'document': open('./payload.txt', 'rb')}
    post_upload_endpoint(SERVER   "exc/sync", path, test_token)


if __name__ == "__main__":
    main()

The above, when run prints out the below. The first line is my sanity check, making sure that the token is being passed correctly, which it is. The second line is the request body.

    Payload:  {'session_uuid': '90f50db9-39b1-476c-9fa4-3b7d2af51ad4', 'client_uuid': '371f291c-2a65-4384-9e01-bf3ee880b777', 'expiration': 'Wed Jun 18 19:20:21 EDT 2022'}
Request body:  b'--0b9b477016594955267c5e7859cc0acb\r\nContent-Disposition: form-data; name="document"; filename="payload.txt"\r\n\r\n"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."\r\n--0b9b477016594955267c5e7859cc0acb--\r\n'

Why is this happening? I search Google and StackOverflow and no one else appears to be reporting this error. My token is gone, and it's replaced with an entire paragraph of "lorem ipsum".

I've been reading through what docs I can find this morning, but it's pretty sparse, and nothing says anything about defaulting to a paragraph of "lorem ipsum".

CodePudding user response:

Even though it is not very clear from the doc, it seems the json and files parameter of requests.post are mutually exclusive.

We can see that when we look at the requests source code here: Link to the github.

In the prepare_body function, body is set to the encoded json, and then if you provide a file, body is overwritten with the content of the file.

Therefore, the lorem ipsum you are seeing is very likely the content of your file ./payload.txt, did you check it ?

To send a file and a json simultaneously, see this answer.

  • Related