相关疑难解决方法(0)

使用Python发送JSON请求

我是Web服务的新手,我正在尝试使用python脚本发送以下基于JSON的请求:

http://myserver/emoncms2/api/post?apikey=xxxxxxxxxxxxx&json={power:290.4,temperature:19.4}
Run Code Online (Sandbox Code Playgroud)

如果我将上述内容粘贴到浏览器中,它会按预期工作.但是,我很难从Python发送请求.以下是我正在尝试的内容:

import json
import urllib2
data = {'temperature':'24.3'}
data_json = json.dumps(data)
host = "http://myserver/emoncms2/api/post"
req = urllib2.Request(host, 'GET', data_json, {'content-type': 'application/json'})
response_stream = urllib2.urlopen(req)
json_response = response_stream.read()
Run Code Online (Sandbox Code Playgroud)

如何将apikey数据添加到请求中?

谢谢!

python json

21
推荐指数
2
解决办法
6万
查看次数

标签 统计

json ×1

python ×1