2

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.

The Grand J
  • 348
  • 2
  • 5
  • 14
JJG
  • 21
  • 3

4 Answers4

1

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
)
dh762
  • 2,259
  • 4
  • 25
  • 44
  • Thanks for the suggestion. We can't add the headers['Content-Type'] = 'multipart/form-data' since it removes the boundary value from the post request. The above suggested approaches didn't work and i tried couple of different combinations of the files parameter and for this one { 'source': (None, open(input_path, 'rb')), 'filename': (None, "report.txt") }, I am receiving -> 'error': "'source' parameter must given as multipart/form-data with type 'file' – JJG Aug 15 '20 at 04:46
  • Even while using data = { 'source': open(filename, 'rb').read(), 'filename': filename } as the data parameter, it throws the same error -> 'error': "'source' parameter must given as multipart/form-data with type 'file' – JJG Aug 15 '20 at 04:54
  • quite a difficult API they have. You can use or inspect the python client : https://github.com/podio/podio-py/blob/master/pypodio2/encode.py – dh762 Aug 15 '20 at 06:58
1

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)
llamaoo7
  • 4,051
  • 2
  • 22
  • 22
0

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
)
dh762
  • 2,259
  • 4
  • 25
  • 44
0

I finally got this working with Podio. Here's the syntax that worked:

my_file = open("<filepath/name>", "rb")
url = 'https://api.podio.com/file/v2'
headers = {
                'authorization': 'OAuth2 <cookie>'
          }
self.data = {
                'filename': <"filename">
            }
self.files = {
                'source': (<"filename">, my_file, 'file')
            }
response = requests.post(url, data=self.data, files=self.files, headers=headers)

my_file.close()
George
  • 101
  • 1
  • 3