使用请求上传文件并发送额外数据 [英] Uploading files using requests and send extra data

查看:44
本文介绍了使用请求上传文件并发送额外数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用请求上传文件.我需要上传一个PDF文件,同时向表单发送一些其他数据,比如作者姓名.

我试过了:

requests.get(url, files = {"file":open("file.txt"), "author" : "me" })

但它不会向表单发送数据.

解决方案

所以我知道您想上传到一个 URL、一个 pdf 文件以及一些额外的参数.

您遇到的第一个错误是您使用的是 .get() 而不是 .post().

我使用的是文档中的示例,您应该阅读该文档.这应该会让你开始:

<预><代码>>>>url = 'http://httpbin.org/post'>>>files = {'file': open('somefile.pdf', 'rb')}>>>值 = {'作者':'约翰史密斯'}>>>r = requests.post(网址,文件=文件,数据=值)

I am trying to upload a file using requests. I need to upload a PDF file and at the same time send some other data to the form like the author's name.

I tried this:

requests.get(url, files = {"file":open("file.txt"), "author" : "me" })

But it doesn't send data to the form.

解决方案

So I understand that you want to upload to a URL, a pdf file along with some extra parameters.

First error that you have is you are using .get() and not .post().

I am using samples from the documentation, which you should go through. This should get you started:

>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('somefile.pdf', 'rb')}
>>> values = {'author': 'John Smith'}
>>> r = requests.post(url, files=files, data=values)

这篇关于使用请求上传文件并发送额外数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆