Reputation: 1
I'm starting to get into Python lately and was wondering if you could post some code about how to to encode a JSON string, send it as an HTTP request to a URL, and parse the response.
Here's some code I've been playing around with:
import os
import json
if os.name == 'nt':
def clear_console():
subprocess.call("cls", shell=True)
return
else:
def clear_console():
subprocess.call("clear", shell=True)
return
def login_call(username, password):
choice = 0
while int(choice) not in range(1,2):
clear_console()
print ('')
print (' Json Calls - Menu')
choice = input('''
1. Login.
Enter Option: ''')
print ('')
choice = int(choice)
if choice == 1:
login_call(username, password)
Upvotes: 1
Views: 480
Reputation: 21849
I wrote an answer to do such a thing for the github api the other day. See my answer here.
In essence, the code boiled down to:
import urllib2
import json
data = {"text": "Hello world github/linguist#1 **cool**, and #1!"}
json_data = json.dumps(data)
req = urllib2.Request("https://api.github.com/markdown")
result = urllib2.urlopen(req, json_data)
print '\n'.join(result.readlines())
Upvotes: 4
Reputation: 11000
The modules you'll want are httplib
or http.client
, depending on your version of Python, and json
. In JSON, the simple loads
and dumps
functions should encode and decode JSON easily for you.
Upvotes: 1