欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 发送post请求

程序员文章站 2024-01-20 19:02:16
...

1 因为archery项目部署在了docker内 首先安装这次改动需要的模块包  

docker 内安装 Python包
 yum install python-pip
 pip install requests

2 接口开发的研发首先提供了他们的url,

1)本地测试接口返回数据错误,是因为没有设置header,对方接受的数据格式为json

python 发送post请求

import requests, json

# url_json = 'http://httpbin.org/post'
url_json = 'http://10.238.160.55:8181/dataModifyStandardize/sqlAnalysis'
data_json = json.dumps({"sqlId":"30518226","sqlContent":"","sqlMemo":"","dbName":"xiaodai"})  # dumps:将python对象解码为json数据
r_json = requests.post(url_json, data_json)
print(r_json)
print(r_json.text)
print(r_json.content)

 <Response [200]>
{"code":-2,"msg":"Content type \u0027application/octet-stream\u0027 not supported"}
b'{"code":-2,"msg":"Content type \\u0027application/octet-stream\\u0027 not supported"}'

2)

添加header格式后本地测试OK

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

url = "http://10.238.160.55:8181/dataModifyStandardize/sqlAnalysis"
data = json.dumps({"sqlId": "30518226", "sqlContent": "", "sqlMemo": "", "dbName": "xiaodai"})
headers = {'Content-Type': 'application/json'}
r_json = requests.request("POST", url=url, headers=headers, data=data)

print(r_json)
print(r_json.text)
print(r_json.content)

 python 发送post请求

3 修改程序代码 并重启 

docker restart

import requests, json
            url = "http://10.238.160.55:8181/dataModifyStandardize/sqlAnalysis"
            data = json.dumps({'sqlId':workflow_id,'sqlContent':sql_content,'sqlMemo':workflow_title,'dbName':db_name})
            #data =  json.dumps({"sqlId":"30518226","sqlContent":"","sqlMemo":"","dbName":"xiaodai"})
            headers = {'Content-Type': 'application/json'}
            r_json = requests.request("POST", url, headers=headers, data = data)

提交一个工单进行测试,接口打印日志与返回数据

python 发送post请求

 

url = "http://10.238.163.125:8001/api/mongo/findOne"

payload = 'query=%7B%22userId%22%3A%2230518226%22%7D'
headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.request("POST", url, headers=headers, data = payload)

 

https://www.cnblogs.com/benben-wu/p/10220006.html

https://www.cnblogs.com/wbw-test/p/11737683.html

https://www.cnblogs.com/android-it/p/9558751.html