Python MultiPart POST格式错误

编程入门 行业动态 更新时间:2024-10-12 16:24:03
本文介绍了Python MultiPart POST格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图弄清楚如何使用Python将MultiPart POST请求写入OneNote.这是我到目前为止所做的:

I'm trying to figure out how to write a MultiPart POST request to OneNote using Python. Here is what I've done so far:

url = ROOT_URL+"pages" headers = {"Content-Type":"multipart/form-data; boundary=MyAppPartBoundary", "Authorization" : "bearer " + access_token} txt = """--MyAppPartBoundary Content-Disposition:form-data; name="Presentation" Content-type:text/html <!DOCTYPE html> <html> <head> <title>One Note Text</title> </head> <body> <p>Hello OneNote World</p> </body> </html> --MyAppPartBoundary-- """ session = requests.Session() request = requests.Request(method="POST", headers=headers, url=url, data=txt) prepped = request.prepare() response = session.send(prepped)

但是,每当我运行它时,都会收到错误响应多部分有效载荷格式错误".我也这样尝试过:

However, whenever I go to run it, I get the error response "The multi-part payload was malformed." I've also tried it like this:

url = ROOT_URL+"pages" headers = {"Content-Type":"multipart/form-data; boundary=MyAppPartBoundary", "Authorization" : "bearer " + access_token} txt = """<!DOCTYPE html> <html> <head> <title>One Note Text</title> </head> <body> <p>Hello OneNote World</p> </body> </html>""" files = {'file1': ('Presentation', txt, 'text/html')} session = requests.Session() request = requests.Request(method="POST", headers=headers, url=url, files=files) prepped = request.prepare() response = session.send(prepped)

同一件事.甚至超级基础:

Same thing. Even super basic:

headers = {"Content-Type":"multipart/form-data; boundary=MyAppPartBoundary", "Authorization" : "bearer " + access_token} files = {'file1': ('filename', 'data', 'text/plain')} r = requests.post(url, headers=headers, files=files)

给出该错误.我在做什么错了?

Gives that error. What am I doing wrong?

推荐答案

第一种预感:确保请求正文中的换行符是CRLF(而不仅仅是LF). RFC 的多部分规范对此非常挑剔

First hunch: Make sure line breaks in your request body are CRLF (not just LF). The RFC spec for multipart is very picky about that

更多推荐

Python MultiPart POST格式错误

本文发布于:2023-10-10 21:28:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   格式   Python   MultiPart   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!