python用requests的post提交data数据以及json和字典的转换

文章讲述了在使用Python的requests库进行POST请求时,根据不同Content-Type(如application/x-www-form-urlencoded和application/json)如何正确设置data或json参数,以及如何将字典转换为JSON格式以便发送。
摘要由CSDN通过智能技术生成

环境:python3.8.10

python使用requests的post提交数据的时候,代码写法跟抓包的headers里面的'Content-Type'有关系。

(一)记录'Content-Type': 'application/x-www-form-urlencoded'的写法。

import requests

url='https://xxx.com'

headers={
    'Content-Type': 'application/x-www-form-urlencoded',
}

my_data = {'value': '{"account":"username","pwd":"pwd","type":"sign","device_info":{"model":"android","screen":"720x1280"},"extra_info":{"app_version":5000},"body":{"uid":"8899","type":1,"signin_day":1,"keep_signin":1}}'}

#简单点的就是
#my_data = {'key1':'value1'}
#或者
#my_data = {'key1':'value1','key2':'value2'}

response = requests.post(url=url, headers=headers,data=my_data)
print(response.text)

(二)记录'Content-Type': 'application/json'的写法

import requests

url='https://xxx.com'

headers={
    'Content-Type': 'application/json',
}

my_data = {'value': '{"account":"username","pwd":"pwd","type":"sign","device_info":{"model":"android","screen":"720x1280"},"extra_info":{"app_version":5000},"body":{"uid":"8899","type":1,"signin_day":1,"keep_signin":1}}'}

#简单点的就是
#my_data = {'key1':'value1'}
#或者
#my_data = {'key1':'value1','key2':'value2'}

response = requests.post(url=url, headers=headers,json=my_data)
print(response.text)


以下是我编程时候的随手记:

(1)POST

#header_car中有一句 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',(表格形式)
data_car={
            "ajax": "1",
            "a": "confproduct",
            "configure": "true",
            "i": "0",
            "hostname": r_name,
            "rootpw": r_pwd,
            "ns1prefix": "ns1",
            "ns2prefix": "ns2",
            "billingcycle": "monthly",
        }
c_url="https://xxx.com"
#有代理 
r_car=s.post(c_url,headers=header_car,proxies=proxies,data=data_car)
#无代理
r_car=s.post(c_url,headers=header_car,data=data_car)

(2)POST(涉及将字典格式转化为json数据的知识)

#如果是请求头部是'Content-Type': 'application/json'

payload = {'simpleSku': 'KS111A016-A110075000', 'anonymous': 0}#这是字典

json_data=json.dumps(payload)#将字典转变为json数据
print json_data#{"simpleSku": "KS111A016-A110075000", "anonymous": 0}这是json格式

r_car=requests.post(c_url,headers=header_car,data=json.dumps(payload))#如果请求头为'Content-Type': 'application/json',需要使用json格式的数据

print r_car.status_code
print r_car.content

还发现了一点,如果字典中有值为false,把false改为0,反而正常运行了。

或者

header_launch={
    'accept': 'application/json',#说明接收的参数是json格式
    'content-type': 'application/json; charset=UTF-8',
}

data_car={
    "country": "CN",
    "id": gdid,
    "productId": productId,
    "skuId": "",#空在这里代表了null
    "wishlistId": wishlistId
    }

url1='https://xxx.com'
result=requests.put(url1,headers=header_launch,json=data_car)#注意:headers不能缺少,json=data_car,data_car为json格式
print(result)

(3)GET

        params=(
                ('jsv','2.4.2'),
                ('appKey','12574478'),
                ('t',millis),
                ('sign',sign),
                ('api','mtop.taobao.shop.impression.intro.get'),
                ('v','1.0'),
                ('type','originaljson'),
                ('secType','1'),
                ('timeout','3000'),
                ('AntiCreep','true'),
                ('dataType','json'),
                ('data',my_data),
                )
        result=requests.get(url_tel,headers=header_shop,params=params)



更多的内容可以参考文章:

一文详解 requests 库中 json 参数和 data 参数的用法_requests data json格式-CSDN博客

python——json、字典的区别及相互转换方法_python json转字典-CSDN博客

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以提供一些指导,使用Pythonrequests模块发送JSON数据的方法如下:首先,安装requests模块,然后使用requests.post()方法将JSON数据发送到你指定的URL。示例代码如下:import requestsurl = 'http://www.example.com/'data = {'key1': 'value1', 'key2': 'value2'}headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}response = requests.post(url, data=json.dumps(data), headers=headers) ### 回答2: 使用Pythonrequests模块发送JSON数据非常简单。下面是一个示例: 首先,我们需要导入requests模块: import requests 然后,我们可以使用requestspost方法来发送JSON数据: url = "http://example.com/api" # 需要发送JSON数据的API地址 data = {"name": "John", "age": 30} # 要发送的JSON数据 headers = {"Content-Type": "application/json"} # 设置请求头,指定数据格式为JSON response = requests.post(url, json=data, headers=headers) 在上面的代码中,我们将url设置为要发送JSON数据的API地址,将data设置为要发送的JSON数据。同时,我们还指定了请求头Content-Type为application/json,以告知服务器我们发送的是JSON格式的数据。 最后,我们使用requestspost方法发送请求,并将json参数设置为data,headers参数设置为headers。服务器将返回一个response对象,我们可以通过它来获取服务器的响应。 对于发送JSON数据,我们可以使用requests模块的json参数来将数据转换JSON格式,并自动设置Content-Type为application/json。 以上就是使用Pythonrequests模块发送JSON数据的简单示例。使用requests模块可以轻松地向服务器发送JSON数据,并处理服务器的响应。 ### 回答3: 使用pythonrequests模块发送JSON数据非常方便。 首先,需要导入requests模块:import requests 然后,准备要发送的JSON数据,可以将字典转换JSON字符串,例如:data = {"name": "小明", "age": 18} 接下来,使用requests模块的post方法发送JSON数据,同时指定请求头的Content-Type为application/json,例如: response = requests.post(url, json=data, headers={"Content-Type": "application/json"}) 其中,url为要发送请求的URL地址,data为要发送的JSON数据,headers为请求头。 最后,可以通过response对象获取服务器的响应结果。例如,可以通过response.text来获取响应的文本结果,response.json()来获取响应的JSON结果。 完整的代码示例: import requests data = {"name": "小明", "age": 18} url = "http://example.com/api" response = requests.post(url, json=data, headers={"Content-Type": "application/json"}) if response.status_code == 200: print("请求成功") print(response.text) print(response.json()) else: print("请求失败") 以上就是使用pythonrequests模块发送JSON数据的简单示例。希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值