简体   繁体   中英

Error while using Podio API - File Upload operation

I am trying to upload a file to Podio using file upload POST operation ( "https://api.podio.com/file/" ) using Python 3.7. Please find below the details about the code.

        # File Upload
        fin = open(input_path, 'rb')
        
        files = {'source': ('report.txt', fin, 'text/plain')}
        uploadResponse = requests.post(PODIO_BASE_URL + "file/", files=files,
                                       headers=bearerHeader)

I did make sure that i followed the contracts mentioned in Podio API documentation

  1. Using multipart/form-data as the content-type
  2. Specifying 'source' parameter with payload content as a byte array
  3. Specifying 'filename' parameter ( the first value in the tuple in the above mentioned code).

I am getting the below error while doing this operation:-

File upload reponse: {'error_parameters': {}, 'error_detail': None, 'error_propagate': False, 'request': {'url': 'http://api.podio.com/file/', 'query_string': '', 'method': 'POST'}, 'error_description': 'Invalid value null (null): must be non empty string', 'error': 'invalid_value'}

Please find below the post request which i captured using the prepare method ( i have purposefully removed the bearer value from the below snippet ).

req.prepare()
<PreparedRequest [POST]>
special variables:
function variables:
body: b'--5e83f2bb93a03c8b128f6158c00863c4\r\nContent-Disposition: form-data; name="source"; filename="report.txt"\r\n\r\ntesting\r\n--5e83f2bb93a03c8b128f6158c00863c4--\r\n'
headers: {'Authorization': 'Bearer ', 'Content-Length': '155', 'Content-Type': 'multipart/form-data; boundary=5e83f2bb93a03c8b128f6158c00863c4'}
hooks: {'response': []}
method: 'POST'
path_url: '/file/'
url: 'https://api.podio.com/file/'
_body_position: None
_cookies: <RequestsCookieJar[]>
_encode_files: <function RequestEncodingMixin._encode_files at 0x0000018437560E18>
_encode_params: <function RequestEncodingMixin._encode_params at 0x0000018437560D90>
_get_idna_encoded_host: <function PreparedRequest._get_idna_encoded_host at 0x000001843756C488>

Any pointers on this would be highly appreciated. Thanks in advance for the help.

you can try

filename = 'report.txt'

data = {
    'source': open(filename, 'rb').read(),
    'filename': filename
}

headers = bearerHeader
headers['Content-Type'] = 'multipart/form-data'

uploadResponse = requests.post(
    PODIO_BASE_URL + "file/", 
    data=data, 
    headers=headers,
    timeout=1200
)

The Podio API doc for file upload says it requires two parameters and requests handles it with the files parameter .

I suggest to try this:

filename = 'report.txt'

multipart_form_data = {
    'source': (filename, open(filename, 'rb')),
    'filename': (filename, None)
}

uploadResponse = requests.post(
    PODIO_BASE_URL + "file/", 
    files=multipart_form_data, 
    headers=bearerHeader,
    timeout=1200
)

I got stuck on this for awhile with only cryptic 500 errors to work with. I eventually compared the raw HTTP request being sent with pypodio2 with what I was sending and determined that the Content-Type was missing for part of the multipart/form-data request. The below works for me (where client.access_token is my OAuth2 token, client.base_url is my domain, and file_path is the local path to a text file). Hopefully this helps someone.

headers = { "authorization": f"OAuth2 {client.access_token}" }
files = { 
    "source": (file_path, open(file_path, 'rb'), "text/plain; charset=utf-8"), 
    "filename": (None, "my_file.txt", "text/plain; charset=utf-8") 
}
response = requests.post(f"{client.base_url}/file", headers=headers, files=files)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM