Last active
February 23, 2017 10:15
-
-
Save ping13/aca4b53e5c4b520851193da7c11bbe22 to your computer and use it in GitHub Desktop.
Example REST calls with python aund urllib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
import urllib2 | |
import json | |
# GET | |
params = urllib.urlencode({'hi':'andre', 'foo': 'bar'}) | |
response = urllib2.urlopen('http://httpbin.org/get?' + params) | |
result = json.loads(response.read()) | |
print json.dumps(result, indent=2) | |
# POST | |
data = {'hi':'andre', 'foo': 'bar'} | |
req = urllib2.Request('http://httpbin.org/post') | |
req.add_header('Content-Type', 'application/json') | |
response = urllib2.urlopen(req, json.dumps(data)) | |
result = json.loads(response.read()) | |
print json.dumps(result, indent=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"origin": "194.124.249.4", | |
"headers": { | |
"Host": "httpbin.org", | |
"Accept-Encoding": "identity", | |
"User-Agent": "Python-urllib/2.7" | |
}, | |
"args": { | |
"hi": "andre", | |
"foo": "bar" | |
}, | |
"url": "http://httpbin.org/get?hi=andre&foo=bar" | |
} | |
{ | |
"files": {}, | |
"origin": "194.124.249.4", | |
"form": {}, | |
"url": "http://httpbin.org/post", | |
"args": {}, | |
"headers": { | |
"Content-Length": "29", | |
"Host": "httpbin.org", | |
"Content-Type": "application/json", | |
"Accept-Encoding": "identity", | |
"User-Agent": "Python-urllib/2.7" | |
}, | |
"json": { | |
"hi": "andre", | |
"foo": "bar" | |
}, | |
"data": "{\"hi\": \"andre\", \"foo\": \"bar\"}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment