Python Forum
Works with Curl. Can't get it to work in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Works with Curl. Can't get it to work in Python
#1
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
Reply
#2
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.
Reply
#3
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)
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code used to work 100%, now sometimes works! muzicman0 5 1,427 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  How do loop over curl and 'put' different values in API call? onenessboy 0 1,219 Jun-05-2022, 05:24 AM
Last Post: onenessboy
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,007 Dec-18-2021, 02:38 AM
Last Post: knight2000
  how can I correct the Bad Request error on my curl request tomtom 8 5,058 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Python Regular expression, small sample works but not on file Acernz 5 2,920 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  "Automate the Boring Stuff with Python" creating a path works but only for CMD promt Milos 2 2,864 Nov-28-2020, 01:08 PM
Last Post: Larz60+
  New to Python, How does this lambda expression works? Joshh_33 2 2,028 Mar-26-2020, 03:32 PM
Last Post: Joshh_33
  curl and jq on python enigma619 3 3,781 Feb-29-2020, 05:54 AM
Last Post: ndc85430
  time.sleep works erratically, a bug in Python? stipcevic 2 3,881 Jan-21-2020, 09:38 PM
Last Post: Marbelous
  How inheritance works in Python ARV 1 1,832 Oct-03-2019, 03:06 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020