本文介绍了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格式错误
发布评论