0

I am new to cURL; I wanted to convert a curl command of this structure:

curl -X POST "http://127.0.0.1:8881/models/faewrfaw/v1/predict" -H "Content-Type:multipart/form-data" -F "data={\"key\": \"Path\"};type=application/json" -F "Path=@C:\Users\rtam\Music\1 2 3\TEST123.jpg"

to a python request. I used https://curl.trillworks.com/ to assist me and got this as the output:

import requests

headers = {
    'Content-Type': 'multipart/form-data',
}

files = {
    'data': (None, '{"key": "Path"};type'),
    'Path': ('C:\\Users\\rtam\\Music\\1 2 3\\TEST123.jpg', open('C:\\Users\\rtam\\Music\\1 2 3\\TEST123.jpg', 'rb')),
}

response = requests.post('http://127.0.0.1:8881/models/faewrfaw/v1/predict', headers=headers, files=files)

However, when testing the response in python I got a bad request/request the server did not understand. I noticed the trillworks website did not account for the type (application/json) in its formatting, does it need to be in the python script?

e0k
  • 6,961
  • 2
  • 23
  • 30
rwt
  • 1
  • Can you include the request error you are getting? – gtalarico May 10 '18 at 17:23
  • So I tried typing in: print(response.text) print(response.status_code) and I got this error with a 400 Bad Request status code: 400 Bad Request

    Bad Request

    The browser (or proxy) sent a request that this server could not understand.

    400
    – rwt May 10 '18 at 17:35
  • I'd recommend comparing the actual request sent in both cases and seeing what's different. There are questions out there for seeing the content of requests from [`requests`](https://stackoverflow.com/questions/10588644/how-can-i-see-the-entire-http-request-thats-being-sent-by-my-python-application) and from [curl](https://stackoverflow.com/questions/3252851/how-to-display-request-headers-with-command-line-curl). – glibdud May 10 '18 at 17:48

1 Answers1

0

This answer might help you: How to send JSON as part of multipart POST-request

In your case it would be

files = {
    'Path': (
        'C:\\Users\\rtam\\Music\\1 2 3\\TEST123.jpg', 
        open('C:\\Users\\rtam\\Music\\1 2 3\\TEST123.jpg', 'rb'), 
        'application/octet-stream'
    )
    'data': (None, '{"key": "Path"}', 'application/json'),
}

response = requests.post(
    'http://127.0.0.1:8881/models/faewrfaw/v1/predict',
    files=files
)
  • Thanks for the link. I saw your changes--taking away the header and adding the octet stream--but I still get the 400 Bad Request

    Bad Request

    The browser (or proxy) sent a request that this server could not understand.

    – rwt May 10 '18 at 18:35
  • Maybe the file name is invalid? Try using just `TEST123.jpg` as the first element in the Path tuple. – JF Rebolledo May 10 '18 at 21:47