Last active
December 18, 2015 22:59
-
-
Save rosner/5857983 to your computer and use it in GitHub Desktop.
I used curl to call transloadit.com's API so I know how the http body should look like. I wanted to use requests to import some images with transloadit.
The key is to use requests PreparedRequest to manipulate the body.
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
curl -F 'params="{"auth": {"key": "YOUR_KEY"}, "template_id"="YOUR_TEMPLATE_ID"}' \ | |
-F "CUSTOM_FORM_FIELD=YOUR_CUSTOM_VALUE" \ | |
-F "FANCY_THIS=FANCY_THAT" \ | |
"http://api2.transloadit.com/assemblies" |
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
------------------------------3d01f393c9e7 | |
Content-Disposition: form-data; name="params" | |
{"auth": {"key": "YOUR_KEY"}, "template_id": "YOUR_TEMPLATE_ID"} | |
------------------------------3d01f393c9e7 | |
Content-Disposition: form-data; name="CUSTOM_FORM_FIELD" | |
YOUR_CUSTOM_VALUE | |
------------------------------3d01f393c9e7 | |
Content-Disposition: form-data; name="FANCY_THIS" | |
FANCY_THAT | |
------------------------------3d01f393c9e7 |
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 json | |
from requests import Request, Session | |
TRANSLOADIT_URL = 'http://api2.transloadit.com/assemblies' | |
session = Session() | |
def import_image(auth_key, params): | |
_params = { | |
'auth': { | |
'key': auth_key | |
}, | |
'template_id': 'YOUR_TEMPLATE_ID' | |
} | |
params.update({ 'params': json.dumps(_params) } ) | |
prepared_request = Request('POST',TRANSLOADIT_URL, files=params).prepare() | |
body = prepared_request.body | |
for param in params.keys(): | |
to_replace = '; filename="%s"\r\nContent-Type: application/octet-stream' % param | |
body = body.replace(to_replace, '') | |
print body | |
prepared_request.body = body | |
response = session.send(prepared_request) | |
print response.content | |
if __name__ == '__main__': | |
params = { | |
'CUSTOM_FORM_FIELD': 'YOUR_CUSTOM_VALUE', | |
'FANCY_THIS': 'FANCY_THAT' | |
} | |
import_image('YOUR_KEY', params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment