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?