Skip to content

Instantly share code, notes, and snippets.

@tom-henderson
Created June 10, 2017 22:30
Show Gist options
  • Save tom-henderson/0b16f79b06367c1135c14667bed0656b to your computer and use it in GitHub Desktop.
Save tom-henderson/0b16f79b06367c1135c14667bed0656b to your computer and use it in GitHub Desktop.
Python HTTP POST with JSON body using api.imgflip.com as example
import requests
url = 'https://api.imgflip.com/caption_image'
payload = {
'username': "imgflip_hubot",
'password': "imgflip_hubot",
'template_id': "101470",
'text1': 'Robots',
}
response = requests.post(url, data=payload)
print response.text
import urllib
url = 'https://api.imgflip.com/caption_image'
payload = {
'username': "imgflip_hubot",
'password': "imgflip_hubot",
'template_id': "101470",
'text1': 'Robots',
}
data = urllib.urlencode(payload)
response = urllib.urlopen(url, data)
print response.read()
import urllib2
from urllib import urlencode
import json
url = 'https://api.imgflip.com/caption_image'
payload = {
'username': "imgflip_hubot",
'password': "imgflip_hubot",
'template_id': "101470",
'text1': 'Robots',
}
data = urlencode(payload)
request = urllib2.Request(url)
request.add_header('User-Agent', "Mozilla/5.0")
response = urllib2.urlopen(request, data)
content = json.loads(response.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment