get/post请求常见content-type种类

本文介绍了HTTP请求的组成部分,并详细讲解了不同Content-Type的用途,包括application/x-www-form-urlencoded、application/json、multipart/form-data、text/plain、text/html和text/xml。get请求中,Content-Type对params的影响较小,而post请求中,应用json和data传递不同类型的数据,并需要正确设置Content-Type,如application/json用于传递JSON数据,multipart/form-data用于文件上传。此外,还强调了在处理text/xml时需遵循XML语法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTTP 请求分为三个部分:状态行、请求头、消息主体

在HTTP协议的消息头中,通常使用Content-Type来表示传值的内容的格式,服务端根据Content-Type字段对获取消息主体的编码方式对消息解析。

get()方法中,通过params传参,可接收的参数类型为dict、list of tuples、bytes。传入的params为字典或字节流形式的会被解析后直接以参数的形式附带在url后面,对于get()方法的content-type不具有太大意义

post()方法中,可通过json和data接收参数
data接收的参数类型dict、list of tuples、bytes、file
json接收的参数为序列化的json对象

application/x-www-form-urlencoded

post():参数是字典类型的话,传入参数用data接收,表示通过表单传参。例如a=1&b=2。使用json接收会报错。

    url = "http://httpbin.org/post"
    data = {"k1": "v1", "k2": "v2"}
    headers = {"Content-type": "application/x-www-form-urlencoded"}
    r = requests.post(url=url, data=data)
    print(r.text)

post请求传入参数类型dict不指定content-type默认application/x-www-form-urlencoded

	url = "http://httpbin.org/post"
    data = {"k1": "v1", "k2": "v2"}
    res = requests.post(url=url, json= data)
    print(res.text)  # content-type:application/json

使用json接收,打印结果的content-type为application/json

get():get只能读取url后面到的param参数,form表单提交不能用get请求

application/json

post():传入参数类型为dict格式用data接收时,需要指定content-type为application/json,否则默认为application/x-www-form-urlencoded;且需要将dict使用json.dumps()转化为json格式的字符串,否则请求报错。
传入参数用json接收时,不用指定content-type,默认为application/json,且不需要转化为json格式的字符串。

def login(self, url, username, password, code):
	passwordstr = getPwd(password)
	requests_type = {
            'password': passwordstr,
            'service': 'XXX',  
            'smsCode': code,
            'userName': username
        }
    header = {'Content-Type': 'application/json'}
    rsp = requests.request(method="post", url = url, json=requests_type, headers=header)
    # 如果使用data接收
    rsp = requests.request(method="post", url = url, data=json.dumps(requests_type), headers=header)

get():传入json数据时,定义字典变量,转入json格式数据,定义相应get请求参数构造get请求参数param,param={‘p’: json} 。此方法可以实现传入json参数,但不推荐。

		dict = {
            '':''  # 填入相应数据
        }
        dict_json = json.dumps(dict)
        param = {"":dict_json}
        response = requests.request("GET", url, params=param)

multipart-formdata

一般是需要在表单里进行文件上传的情况

post():文件等二进制形式使用data接收

get():get方法中的params参数在发送请求时是可选的状态,可不写params参数额外使用files参数接收

	params = OrderedDict([("username", (None, '130533193203240022')),
                          ("password", (None, 'qwerqwer')),
                          ('captchaId', (None, 'img_captcha_7d96b3cd-f873-4c36-8986-584952e38f20')),
                          ('captchaWord', (None, 'rdh5')),
                          ('_csrf', (None, '200ea95d-90e9-4789-9e0b-435a6dd8b57b'))])

    res = requests.get('http://www.baidu.com', files=params)
    print (res.request.body)

text/plain、text/html、text/xml

post():参数是str类型时,使用data接收参数
注意:text/xml 时必须按照 xml 的语法写

get():

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值