Python Forum

Full Version: Works with Curl. Can't get it to work in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am not sure what I have missed, but I can't get this Curl command to work with the code below. What am I missing? I am getting a 400 returned.

The Curl is

curl -X POST "URL" -H "Authorization: token" \
-H "X-Correlation-ID: Optional" \
-F "file=@/Users/Shared/Reports/file.exe" \
-F "report_format=html"

My code is

    access_token = authenticate(client_id, client_secret)
    query_url = 'URL'
    headers = {'Authorization': access_token
               # 'Accept': 'application/json SUCCESS',
               # 'Content-Type': 'multipart/form-data'
               }
    files = {
        'file': '@/Users/Shared/Reports/file.exe',
        'report_format': 'html'
    }
    # This is optional
    # headers['X-Correlation-ID'] = 'optional'
    request_result = requests.post(
        query_url,
        headers=headers,
        files=files
    )
   result = request_result.json()
    return
I have made some progress. The below will give me a 200.

files = {
        'file': open('/Users/Shared/Reports/file.exe', 'rb'),
        # 'report_format': 'html'
    }
but I need to send the report format as the default is JSON and I want HTML.
Can try this.
import requests

headers = {
    'Authorization': 'token',
    'X-Correlation-ID': 'Optional',
}

files = {
    'file': ('/Users/Shared/Reports/file.exe', open('/Users/Shared/Reports/file.exe', 'rb')),
    'report_format': (None, 'html'),
}

response = requests.post('http://URL', headers=headers, files=files)
That did indeed work. Thank you very much. Can you please tell me why.

I had to remove this line

result = request_result.json()
as I assume I am no longer getting json back. I need to get the HTML out of the response.