美文网首页
http 请求的几种数据格式

http 请求的几种数据格式

作者: 王镇_ee87 | 来源:发表于2021-01-13 09:05 被阅读0次

http协议中几种传递数据方式
一 、application/x-www-form-urlencoded

1 requests.post(url='',data={'key1':'value1','key2':'value2'},headers={'Content-Type':'application/x-www-form-urlencoded'})

♦Reqeusts支持以form表单形式发送post请求,只需要将请求的参数构造成一个字典,然后传给requests.post()的data参数即可。

url = 'http://httpbin.org/post'
d = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=d)
print r.text

二、multipart/form-data
除了传统的application/x-www-form-urlencoded表单,我们另一个经常用到的是上传文件用的表单,这种表单的类型为multipart/form-data
形式:

1 requests.post(url='',data={'key1':'value1','key2':'value2'},headers={'Content-Type':'multipart/form-data'})

代码

from requests_toolbelt import MultipartEncoder
import requests

m = MultipartEncoder(
    fields={'field0': 'value', 'field1': 'value',
            'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
    )

r = requests.post('http://httpbin.org/post', data=m,
                  headers={'Content-Type': m.content_type})

三、application/json
请求正文是raw
♦传入xml格式文本

requests.post(url='',data='<?xml  ?>',headers={'Content-Type':'text/xml'})

♦传入json格式文本

requests.post(url='',data=json.dumps({'key1':'value1','key2':'value2'}),headers={'Content-Type':'application/json'})

四、text/xml
请求正文是binary

1 requests.post(url='',files={'file':open('test.xls','rb')},headers={'Content-Type':'binary'})

相关文章

  • 网络(三):应用层HTTP

    目录一、字符编码和各个码表二、HTTP 1、HTTP的数据格式  1.1 HTTP请求的数据格式 = 请求行 + ...

  • Android http的请求体body的几种数据格式

    1、http的请求体body的几种数据格式 1.1 multipart/form-data 以表单形式提交,主要是...

  • http 请求的几种数据格式

    http协议中几种传递数据方式[https://www.cnblogs.com/insane-Mr-Li/p/91...

  • HTTP_POST请求的数据格式

    HTTP_POST请求的数据格式 在HTTP的请求头中,可以使用Content-type来指定不同格式的请求信息。...

  • 接口测试(一)

    http协议内容 请求请求头请求行请求正文 响应响应头响应行响应正文 请求正文数据格式键值对XMLJSON 熟悉项...

  • Tomcat - request 和 response对象

    Request对象 HTTP请求消息数据格式(记忆:请求-->出门-->带上行头和身体) request对象封装了...

  • Http协议

    作用:- 对浏览器客户端与服务器的数据格式的规范!! 1、http请求 2.http响应 请求方式Get VS ...

  • http 几种请求方式

    根据HTTP标准,HTTP请求可以使用多种请求方法。 HTTP1.0定义了三种请求方法: GET, POST 和 ...

  • 2018-08-23 B/S架构——接口规范

    接口规范: 约定前后端交互方式和数据格式 主要内容有 协议类型 : http/https 请求类型 : get请求...

  • golang http 客户端

    http 发送post 请求, 发送数据格式用application/json,发送参数必须写到buffer 缓冲...

网友评论

      本文标题:http 请求的几种数据格式

      本文链接:https://www.haomeiwen.com/subject/opobwktx.html