나는'm 새로운 웹 서비스도를 보내려고 다음과 같은 JSON 기반으로 요청을 사용하여 python 스크립트:
http://myserver/emoncms2/api/post?apikey=xxxxxxxxxxxxx&json={power:290.4,temperature:19.4}
면을 붙여 위의 브라우저로,그것은 예상대로 작동합니다. 그런데,나는 고군분투하는 요청을 보내에서 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()
에 추가하려면 어떻게 해야 하 apikey 가으로 데이터를 요청이 있으십니까?
감사합니다!
를 사용하는 대신 urllib2 사용할 수 있습니다requests. 이 새로운 python lib 이다 정말로 잘 작성하고 그's 이 더 쉽고 직관적으로 사용할 수 있습니다.
을 보낼 json 데이터 같은 것을 사용할 수 있습니다 다음 코드:
import json
import requests
data = {'temperature':'24.3'}
data_json = json.dumps(data)
payload = {'json_payload': data_json, 'apikey': 'YOUR_API_KEY_HERE'}
r = requests.get('http://myserver/emoncms2/api/post', data=payload)
을 검사할 수 있습r
을 얻 http 상태코드,내용,etc.
비록 이 정확하게 답 작문,그것은 여기에서 언급하는 요청의 모듈은
json`옵션을 사용할 수 있는 다음과 같다:
``python
가져오는 요청
요청합니다.포스트( 'http://myserver/emoncms2/api/post?apikey=xxxxxxxxxxxxx', json={"온":"24.3"} )
는 것에 해당하는 컬:
bash
컬'http://myserver/emoncms2/api/post?apikey=xxxxxxxxxxxxx'\
-H'Content-Type:응용 프로그램/json'\
--데이터는 바이너리'{"온":"24.3"}'
``