Skip to content

Instantly share code, notes, and snippets.

@ryanchang
Last active August 29, 2015 14:26
Show Gist options
  • Save ryanchang/0ead8f7efd274ecbf0e0 to your computer and use it in GitHub Desktop.
Save ryanchang/0ead8f7efd274ecbf0e0 to your computer and use it in GitHub Desktop.
Upload audio file to rails server with paperclip with python requests HTTP client.
import requests
import base64
from datetime import datetime
import json
f = open('output.mp3')
recording = base64.b64encode(f.read())
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
payload = {'audio_clip': {'device':'0005', 'time':current_time, 'recording':{'filename':'output.mp3', 'content_type':'audio/mp3', 'content':recording}}}
headers = {'content-type':'application/json'}
response = requests.post('http://localhost:7000/api/audio_clips', data=json.dumps(payload), headers=headers)
print response.text
print response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment