pythonstreamlit

Error while sending a file through python code but not through POSTMAN


Im trying to upload a file through a streamlit app but get the following error:Reads the file as a dir

The Streamlit Code To Upload

    if uploaded_file is not None:
        fname = uploaded_file.name
        img = Image.open(uploaded_file) # Open image in PIL
        col1.image(img)     #Display the Imgage on ST
        img.save(b, 'jpeg') #Send image to the io module
        img = b.getvalue()  #Convert to bytes
        res = self.request(img)

Code to Call API:

class response():

  def crackD(f):

    body = {'deviceSize':('', '(0.0, 0.0, 375.0, 667.0)') ,'ConsumerID':('', '-100'), 'client':'crackd-api', 'metadata':('', '{"source": "streamlit_test"}'), 'file': f} #Body of the post    
    headers = {"Postman-Token":"xxx","cache-control":"no-cache","x-access-token":"xxx" }  
    response = requests.post(xxx, headers = headers, files=body)         
    return response

       

But the api is being called successfully through POSTMAN..


Solution

  • From the error information you provided, the reason is that the uploaded content is a folder, not a simple file.

    You can refer to the official file upload example to check your problem.

    https://docs.streamlit.io/library/api-reference/widgets/st.file_uploader