0

I'm uploading a file to a POST API endpoint which has not only simple key:value pairs, but also has an array field which consists of nested elements, where one of the nested fields is the file. An example is below:

simpleField1: string,
simpleField2: int,
complexField: [
  nestedField: string,
  fileField: string<binary>
  ...
]
...

I can use the requests library similar to this post - by using the files field, we can set the multipart/form-data content type. From the docs, it seems like the file object needs to be a specific content, but the complexField consists of both binary (i.e. the file) and plain text.

How can I encode my complex field via the requests library and/or curl? Thanks!

I've tried the following, but the format seems to be rejected by the server:

files = {
  'complexField': [
    'nestedField': (None, 'some_value'),
    'fieldField: (None, open('input.json', 'rw')
  ]
}
requests.post(url, files=files)

0 Answers0