为什么我的帖子请求带有python的请求库,其中填充了“ lorem ipsem”。文本?

发布于 2025-02-06 05:20:10 字数 2037 浏览 0 评论 0原文

我正在研究一个较大的脚本,并在尝试与JSON令牌一起发布文件时遇到了一个奇怪的问题。

我已经成功使用了requests.post()函数将JSON数据发送到服务器。 HOWERVER,当我尝试发送文件和JSON数据时,帖子失败了,当我检查帖子的正文时,它充满了我没有放在那里的“ Lorem Ipsum”文本,这会导致fial请求,我JSON数据找不到哪里。

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()

以上时,运行时会打印出下面。第一行是我的理智检查,请确保将令牌正确通过,这是我的理智检查。第二行是请求主体。

    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'

为什么会发生这种情况?我搜索Google和Stackoverflow,似乎没有其他人报告此错误。我的令牌已经消失了,它被“ Lorem Ipsum”的整个段落所取代。

我一直在阅读今天早上可以找到的文档,但这很稀疏,没有什么可以说默认为“ Lorem Ipsum”的段落。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2025-02-13 05:20:10

即使从文档中不太清楚,似乎json文件 requests.post的参数是相互排斥的。

我们可以看到,当我们在此处查看请求源代码时:

prepar_body函数中,将正文设置为编码的JSON,然后如果提供文件,则主体将被文件的内容覆盖。

因此,您看到的lorem ipsum很可能是文件的内容./ pareload.txt,您检查了吗?

要同时发送文件和JSON,请参见此答案

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文