1

Python实战:使用requests通过post方式提交json数据

 1 year ago
source link: https://blog.51cto.com/mouday/5808792
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python实战:使用requests通过post方式提交json数据

精选 原创
pip install requests

该方式比较简单,可以直接提交json参数提交

# -*- coding: utf-8 -*-

import requests

url = 'http://httpbin.org/post'
data = {
    'name': 'Tom',
    'age': 20
}

res = requests.post(url, json=data)
print(res.text)
{
  "args": {}, 
  "data": "{\"name\": \"Tom\", \"age\": 20}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "26", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.28.1", 
    "X-Amzn-Trace-Id": "Root=1-635f3812-432dff7a0625e9331bb8d78d"
  }, 
  "json": {
    "age": 20, 
    "name": "Tom"
  }, 
  "origin": "1.202.253.34", 
  "url": "http://httpbin.org/post"
}

通过自定义请求体提交json数据

# -*- coding: utf-8 -*-

import json

import requests

url = 'http://httpbin.org/post'
data = {
    'name': 'Tom',
    'age': 20
}

# 先序列化成json数据
data_raw = json.dumps(data)
print(data_raw)
# {"name": "Tom", "age": 20}

res = requests.post(url, data=data_raw)
print(res.text)

{
  "args": {}, 
  "data": "{\"name\": \"Tom\", \"age\": 20}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "26", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.28.1", 
    "X-Amzn-Trace-Id": "Root=1-635f38b0-37b9f6261c85922202d35ee8"
  }, 
  "json": {
    "age": 20, 
    "name": "Tom"
  }, 
  "origin": "61.48.42.110", 
  "url": "http://httpbin.org/post"
}

进一步优化

我们可以发现,方式二提交的数据缺少了一个请求头

{
    "Content-Type": "application/json"
}

虽然可以识别为json数据,不过有的后端框架有可能不能够正常识别json数据

# -*- coding: utf-8 -*-

import json

import requests

url = 'http://httpbin.org/post'
data = {
    'name': 'Tom',
    'age': 20
}

# 先序列化成json数据
data_raw = json.dumps(data)
print(data_raw)
# {"name": "Tom", "age": 20}

# 增加请求头
headers = {
    "Content-Type": "application/json; charset=UTF-8"
}

res = requests.post(url, data=data_raw, headers=headers)
print(res.text)

{
  "args": {}, 
  "data": "{\"name\": \"Tom\", \"age\": 20}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "26", 
    "Content-Type": "application/json; charset=UTF-8", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.28.1", 
    "X-Amzn-Trace-Id": "Root=1-635f3956-7d2bf56b737dd06278d54b80"
  }, 
  "json": {
    "age": 20, 
    "name": "Tom"
  }, 
  "origin": "61.48.42.110", 
  "url": "http://httpbin.org/post"
}
  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK