Created
June 10, 2017 22:30
-
-
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
This file contains hidden or 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 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 |
This file contains hidden or 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 | |
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() |
This file contains hidden or 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 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