python requests.post带head和body

# coding = utf-8
import requests
import json

host = "http://47.XX.XX.XX:30000"
endpoint=r"/api/v1/carXX/addCarXX"

url = ''.join([host,endpoint])
headers = \
    {
        "X-Member-Id": "23832170000",
        "X-Region": "1100000",
        "X-Channel": "01",
        "Content-Type": "application/json;charset=UTF-8"
    }
body = \
    {
        "designerId": "1111",
        "itemQuantity": 1,
        "sku": "000100000"
    }
r = requests.post(url,headers=headers,data=json.dumps(body))
#r = requests.post(url,headers=headers,json=body)
print r.text
print r.url
  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的例子,演示如何使用Python调用API并在前端中显示结果: 1. 在Python中编写一个函数来调用API ```python import requests def get_weather(city): url = 'https://api.openweathermap.org/data/2.5/weather?q={}&appid=API_KEY'.format(city) response = requests.get(url) data = response.json() return data ``` 该函数使用requests库向OpenWeatherMap API发送请求,并返回JSON格式的响应数据。 2. 在Flask中创建一个简单的Web应用程序 ```python from flask import Flask, render_template, request app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/weather', methods=['POST']) def weather(): city = request.form['city'] data = get_weather(city) temperature = data['main']['temp'] description = data['weather'][0]['description'] return render_template('weather.html', temperature=temperature, description=description) ``` 该应用程序包含两个路由:主页和天气页面。主页使用模板引擎渲染HTML页面,而天气页面根据用户输入的城市名称调用get_weather函数并显示温度和天气描述。 3. 创建HTML模板 ```html <!DOCTYPE html> <html> <head> <title>Weather App</title> </head> <body> <form method="POST" action="/weather"> <label for="city">Enter city:</label> <input type="text" id="city" name="city"> <input type="submit" value="Get Weather"> </form> </body> </html> ``` 该模板包含一个简单的表单,用户可以在其中输入城市名称并提交。 4. 创建另一个HTML模板来显示天气信息 ```html <!DOCTYPE html> <html> <head> <title>Weather App - {{ temperature }}°F, {{ description }}</title> </head> <body> <h1>{{ temperature }}°F</h1> <p>{{ description }}</p> </body> </html> ``` 该模板显示当前温度和天气描述。 5. 运行应用程序 在命令行中运行以下命令启动Flask应用程序: ``` FLASK_APP=app.py flask run ``` 现在,您可以在浏览器中访问应用程序并输入城市名称来获取天气信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值