HTTP.post() Exceptions.StatusError(400, ...)

I’m trying to code the following Python code in Julia:

import requests

# Define the API endpoint URL
url = 'https://example.com/create/'
# Define your API credentials
username = 'john.smith@example.com'
api_key = '1234'

# Define the request headers
headers = {
    'authorization': f'ApiKey {username}:{api_key}',
    'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary2AegpCQ',
}

# Define the request data payload
data = {
    'format': '0', # 0 for CSV
    'data_source': '15',
    'end_date': '1699506000000', # Start time timestamp in milliseconds
    'start_date': '1698811200000', # End time timestamp in milliseconds
    'participant_ids': '123,45678,90123,4567,8901,2345', # List of participant IDs
    'site_ssid': 'null',
    'study_id': '1234', # Study ID
}
 
# Send the POST request

response = requests.post(url, headers=headers, data=data)

This is what I have as the Julia code:

using HTTP

# Define the API endpoint URL
url = "https://example.com/create/"

# Define your API credentials
username = "john.smith@example.com"
api_key = "1234"

# Define the request headers
headers = Dict(
    "Authorization" => "ApiKey $username:$api_key",
    "Content-Type" => "multipart/form-data; boundary=----WebKitFormBoundary2AegpCQ"
)

# Define the request data payload
data = Dict(
    "format"=> "0", # 0 for CSV
    "data_source"=> "15", # 15 for GPS and 16 for Battery
    "end_date"=> "1699506000000", # Start time timestamp in milliseconds
    "start_date"=> "1698811200000", # End time timestamp in milliseconds
    "participant_ids"=> "123,45678,90123,4567,8901,2345", # List of participant IDs
    "site_ssid"=> "null",
    "study_id"=> "1234" # Study ID
)

# Send the POST request
response = HTTP.request("POST", url, headers, data)

It works fine in Python, but in Julia, I get the error, HTTP.Exceptions.StatusError(400, "POST",...

I have tried writing headers and data as Vector{Pair} , which gives me the error, HTTP.RequestError:ERROR: TaskFailedException.

I have tried using HTTP.Form() around the headers and data variables, but that also gives an error.

Does anyone know what I’m doing wrong?

The link that you are trying to open does not exist as ERROR code is 400.

I did not write the actual link for security purposes. The link works if I use Python.
This is a description of the 400 error: “The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).”
Is there anything wrong with my code that fits the above description?

Did you try to use API Reference · HTTP.jl? HTTP.jl should automatically handle the content type header in this case.

You can also try the suggestion from https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026/2 to compare the two (Python and Julia).