Tallme2
Tallme2

Reputation: 53

Issue sending multipart/form-data

I'm attempting to utilize BIMTrack's REST API to post an image. To do this the API requires me to send a json file with but prior to the image, inherently requiring multipart/form-data.

Failure post the json fill will be met with the error code: 415 and error message: The content-type of the first file of the request must be application\json.

I've successfully made this post request using the web debugging proxies of Postman & Fiddler but am unable to repeat my successes within python requests.

Python Code (This doesn't work) :

image = r"C:\Users\aflemming\Desktop\Images\DBMICon.png"
jsonFile = r"C:\Users\aflemming\source\repos\IfcOpenShell\IfcOpenShell\BIM\myjson.json"

headers = {'Authorization' : 'Bearer <MyToken>'}

files = {
    'Json': (None, open(jsonFile, 'rb'), 'application/json'),
    'Image': (None, open(image, 'rb'), 'image/png')
}

r = requests.post(https://api.bimtrackapp.co/v3/hubs/07La7cOZ/projects/20767/issues/3161484/viewpoints, files=files, headers=headers)

Fiddler Raw Request (This works) :

User-Agent:Fiddler Everywhere
Authorization:Bearer eb5e3983a7546dad76067418ff93175ef42b816dd57f78f54101f0b63862542e
Host:api.bimtrackapp.co
Content-Length:11322
Content-Type:multipart/form-data;boundary=-------------------------acebdf13572468

---------------------------acebdf13572468
Content-Disposition: form-data; name="description" 
the_text_is_here
---------------------------acebdf13572468
Content-Disposition: form-data; name="jsonfile"; filename="myjson.json"
Content-Type: application/json

<@INCLUDE *C:\Users\aflemming\source\repos\IfcOpenShell\IfcOpenShell\BIM\myjson.json*@>
---------------------------acebdf13572468
Content-Disposition: form-data; name="image"; filename="DBMICon.png"
Content-Type: image/png

<@INCLUDE *C:\Users\aflemming\Desktop\Images\DBMICon.png*@>
---------------------------acebdf13572468--

Postman Request (This also works): enter image description here

enter image description here

BIMTrack's REST API: https://api.bimtrackapp.co/swagger/ui/index

I'm happy to provide more information where required.

Upvotes: 1

Views: 1285

Answers (1)

Tallme2
Tallme2

Reputation: 53

I found a solution by altering the request method (using requests.request over requests.post) and setting the verify=False parameter.

It seems as though the request was encountering an SSLCertVerificationError and bypassing the certificate resolved this.

Final Code:

image = r"C:\Users\aflemming\Desktop\Images\DBMICon.png"
jsonFile = r"C:\Users\aflemming\source\repos\IfcOpenShell\IfcOpenShell\BIM\myjson.json"
url = "https://api.bimtrackapp.co/v3/hubs/07La7cOZ/projects/20767/issues/3161484/viewpoints"

files = [
    ('Json', ('Json2', open(jsonFile,'rb'), 'application/json')),
    ('Image', ('Image2', open(image,'rb'), 'image/png'))
]

headers = {
    'Authorization': 'Bearer <MyToken>'
}

response = requests.request("POST", url, headers=headers, files = files, verify=False)

Upvotes: 1

Related Questions