Closed ruotianluo closed 5 years ago
Here is python code I had generated from postman. This uses a local file called 3.mp3 to upload. I verified that it works!
import http.client
conn = http.client.HTTPConnection("www,buzzsprout,com")
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\nMy Audio File API Test\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"description\"\r\n\r\nThis is the description of my awesome episode\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"summary\"\r\n\r\nCheck out this summary\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"artist\"\r\n\r\nJohnny Mayher\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"tags\"\r\n\r\nawesome,incredible,amazing\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"published_at\"\r\n\r\n2019-01-15 04:00:00 -0500\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"hq\"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"episode_number\"\r\n\r\n12\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"season_number\"\r\n\r\n7\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"private\"\r\n\r\nfalse\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"explicit\"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"audio_file\"; filename=\"3.mp3\"\r\nContent-Type: audio/mpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'Authorization': "Token token=f6ca2e279b51f417d5fe6fe33929",
'Content-Type': "application/x-www-form-urlencoded",
'User-Agent': "PostmanRuntime/7.16.3",
'Accept': "*/*",
'Cache-Control': "no-cache",
'Postman-Token': "bb4c2614-b5f4-44a4-86ab-f44f89e4cb48,ab1486cc-ab9c-4fe2-aada-e9738fdb9511",
'Host': "www.buzzsprout.com",
'Accept-Encoding': "gzip, deflate",
'Content-Length': "139331",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
conn.request("POST", "api,123,episodes", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Thank you for your rapid response, I will try it out
I got
Traceback (most recent call last):
File "test_api2.py", line 50, in <module>
conn.request("POST", "https://www.buzzsprout.com/api/632479/episodes.json", payload, headers)
File "/opt/conda/lib/python3.7/http/client.py", line 1244, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/opt/conda/lib/python3.7/http/client.py", line 1290, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/opt/conda/lib/python3.7/http/client.py", line 1239, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/opt/conda/lib/python3.7/http/client.py", line 1026, in _send_output
self.send(msg)
File "/opt/conda/lib/python3.7/http/client.py", line 966, in send
self.connect()
File "/opt/conda/lib/python3.7/http/client.py", line 938, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/opt/conda/lib/python3.7/socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/opt/conda/lib/python3.7/socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
Do I need to modify anything to run it?
I'm not a python programmer but you can look up that error code socket.gaierror
on StackOverflow. I also can't tell without seeing the original code.
Thanks
Hi,
I saw there is a way to upload a local audio file as an attachment. Can you elaborate on how to do that?
For example, how to do it in python?