python发送post请求,Springboot接收。

python脚本:

1.post的body是json类型,也可以用json参数传入。
2.先导入json模块,用dumps方法转化成json格式。
3.返回结果,传到data里

#导入请求包
import requests
#导入json包
import json
#设置要访问的地址
url = 'http://10.10.202.68:8585/SingleCell/singcell/pythonToJavaTest'
 
#定一个字典类型
mapA = {
     "account":"lgn","passWord":"123456"}
 
#把字典转成字符串
str = json.dumps(mapA)
 
#这里得给设置一个请求格式,不然会返回415
#header_dict = {
     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',"Content-Type": "application/json"}
header_dict = {
     "Content-Type":"application/json; charset=utf8"}
 
print(str)
 
#直接请求
r = requests.post(url,data=str,headers=header_dict)
#这里是输出了一个字符串
print(r.text)

springboot 接收json:

    @ApiOperation("python发送消息")
    @Log(title = "python发送消息接口测试", businessType = BusinessType.OTHER)
    @PostMapping("/pythonToJavaTest")
    public AjaxResult pythonToJavaTest(@RequestBody JSONObject jsonParam) {
     

        System.out.println("---jsonParam:"+jsonParam.toString());

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("info","Java客户端接收到了消息!");

        AjaxResult ajax = AjaxResult.success();
        ajax.put("success", jsonObject);

        return ajax;
    }

执行python脚本:

[root@localhost: /home/pipeline/V1/test]$ python3 http_post.py
{
     "account": "lgn", "passWord": "123456"}
{
     "msg":"操作成功","code":200,"success":{
     "info":"Java客户端接收到了消息!"}}

Java后台接收:

---jsonParam:{
     "passWord":"123456","account":"lgn"}

你可能感兴趣的:(python,springboot,Java,python,java,restful)