Uploading A Local File With HTTP POST

noellarkin

Senior Member
Joined
Mar 14, 2021
Messages
1,006
Reaction score
1,492
For some reason, the answer to this has been impossible for me to find on StackOverflow.
I want to upload a local file to an API.
The file is located at C:\samplefolder\samplefile.wav

The API documentation tells me to upload it like this:
Python:
import requests
filename = "/path/to/foo.wav"
def read_file(filename, chunk_size=5242880):
    with open(filename, 'rb') as _file:
        while True:
            data = _file.read(chunk_size)
            if not data:
                break
            yield data

headers = {'authorization': "YOUR-API-TOKEN"}
response = requests.post('https://api.assemblyai.com/v2/upload',
                        headers=headers,
                        data=read_file(filename))

print(response.json())

Here's the part I don't get. filename = "/path/to/foo.wav" is how every single file upload example on the internet seems to do it. But filename = "C:\samplefolder\samplefile.wav" doesn't seem to work.
How do I upload a local file from my hard drive, and why is it so effin' hard to find an example of this on the internet?
 
Update:
I also tried formatting the file as
file:///C://samplefolder//samplefile.wav - - if I enter this in my browser navigation bar it opens the file.
but yeah doesn't work for the API endpoint either.
 
Can you please tell me what error your are getting while uploading the file?
 
Need to figure out if you error Is in the request of when finding the file.. are you able to see the http response?
 
It's happening with every single code snippet that has to do with uploading a file to an API endpoint, not just the example snippet I used.
I'm using Insomnia (similar to Postman) to test requests now.
If I use the "multipart form" format, with params
Content-Disposition: form-data
name: file
filename: file:///C://samplefolder//samplefile.wav
and with the Header
Content-Type: multipart/form-data

I get errors like:
{
"error": "File not found! Attach a file to your request."
}

What's frustrating is every example I see has something like filename: samplefile.wav
How does one set the working folder?
 
I'd have expected C://samplefolder//samplefile.wav to work. Try executing your script from the same folder as the file is located, and set the path to either the file name samplefile.wav or ./samplefile.wav
 
Python:
import requests
import os
filename = f"{os.getcwd}/path/to/foo.wav"
def read_file(filename, chunk_size=5242880):
    with open(filename, 'rb') as _file:
        while True:
            data = _file.read(chunk_size)
            if not data:
                break
            yield data

headers = {'authorization': "YOUR-API-TOKEN"}
response = requests.post('https://api.assemblyai.com/v2/upload',
                        headers=headers,
                        data=read_file(filename))

print(response.json()

Maybe?
what i did was added os.getcwd() to get current working directory

or if you want to make an absolute path try

Python:
import requests

import os

filename = f"{os.path.abspath}/path/to/foo.wav"

def read_file(filename, chunk_size=5242880):

    with open(filename, 'rb') as _file:

        while True:

            data = _file.read(chunk_size)

            if not data:

                break

            yield data

headers = {'authorization': "YOUR-API-TOKEN"}

response = requests.post('https://api.assemblyai.com/v2/upload',

                        headers=headers,

                        data=read_file(filename))
print(response.json()
 
Last edited:
Back
Top