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

**URL:** <https://discourse.julialang.org/t/http-post-exceptions-statuserror-400/106251>\
**Category:** New to Julia\
**Tags:** http\
**Created:** [November 15, 2023, 7:42am UTC](https://discourse.julialang.org/t/http-post-exceptions-statuserror-400/106251 "2023-11-15T07:42:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alicepiller](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@alicepiller](https://discourse.julialang.org/u/alicepiller)\
**Post date:** [November 15, 2023, 7:42am UTC](https://discourse.julialang.org/t/http-post-exceptions-statuserror-400/106251/1 "2023-11-15T07:42:10Z")

</div>

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

```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:

```julia
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?

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [November 15, 2023, 7:54am UTC](https://discourse.julialang.org/t/http-post-exceptions-statuserror-400/106251/2 "2023-11-15T07:54:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alicepiller](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@alicepiller](https://discourse.julialang.org/u/alicepiller)\
**Post date:** [November 15, 2023, 7:59am UTC](https://discourse.julialang.org/t/http-post-exceptions-statuserror-400/106251/3 "2023-11-15T07:59:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 15, 2023, 8:04am UTC](https://discourse.julialang.org/t/http-post-exceptions-statuserror-400/106251/4 "2023-11-15T08:04:16Z")

</div>

Did you try to use [API Reference · HTTP.jl](https://juliaweb.github.io/HTTP.jl/stable/reference/#HTTP.Forms.Form)? 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](https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026/2) to compare the two (Python and Julia).
