# Send file with HTTP.post

**URL:** https://discourse.julialang.org/t/send-file-with-http-post/91932
**Category:** General Usage
**Tags:** question, http, file
**Created:** [December 21, 2022, 7:49am UTC](https://discourse.julialang.org/t/send-file-with-http-post/91932 "2022-12-21T07:49:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sardinecan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sardinecan/32/29141_2.png) [@sardinecan](https://discourse.julialang.org/u/sardinecan)
#### Post date: [December 21, 2022, 7:49am UTC](https://discourse.julialang.org/t/send-file-with-http-post/91932/1 "2022-12-21T07:49:33Z")

</div>

Hi everyone,

I’m looking to upload files via an API using Julia with the HTTP.jl package. Unfortunately, the API can’t retrieve my file, and I get an error:

> {“code”:500,“message”:“Unable to retrieve file from request.”,“payload”:}

Here is my code (the API key is a public one for the public test API, anyone can try ;))

```julia
using HTTP

url = "https://apitest.nakala.fr/datas/uploads"

headers = 
    Dict(
        "X-API-KEY" => "01234567-89ab-cdef-0123-456789abcdef", 
        "accept" => "application/json"
    )

fileOpened = open(PATH_TO_THE_FILE, "r")    

fileCur = Dict("file" => fileOpened)

response = HTTP.post(url, files=fileCur, headers=headers)

```

I wrote a similar script with Python and everything seems to work:

```python
import requests

url = "https://apitest.nakala.fr/datas/uploads"

headers = {"X-API-KEY":"01234567-89ab-cdef-0123-456789abcdef", "accept": "application/json"}

fileOpened = open(PATH_TO_THE_FILE, "rb")    

fileCur = {'file': fileOpened}

response = requests.post(url, files=fileCur, headers=headers)

print(str(response.text))

```

The (good) response from the server:

> {“name”:“myFile.txt”,“sha1”:“35b7740a641464a2dfecd282ba0592a8a23f65a3”}

I must have made a mistake with objects sent with HTTP.post but I don’t know how to fix it… Does anyone have any ideas?

Thanks a lot!!  
Josselin

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [December 21, 2022, 9:49am UTC](https://discourse.julialang.org/t/send-file-with-http-post/91932/2 "2022-12-21T09:49:32Z")

</div>

`HTTP.jl`’s API is different from Python’s `requests`. In particular, `HTTP.request` (`post` is a convenience method for `request`), doesn’t know the keyword arguments `files` and `headers`.

Instead it is called as `HTTP.post(url, headers, body)`.

I believe something like

```julia
body = HTTP.Form(Dict(:file => fileOpened))

```

would do the trick. See [API Reference · HTTP.jl](https://juliaweb.github.io/HTTP.jl/stable/reference/#HTTP.Forms.Form)

Your `headers` seem fine, but maybe you need to declare

```plaintext
Content-Type => multipart/form-data

```

No guarantees though, I’m not an expert.

---

<div class="post-metadata">

### Author: ![sardinecan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sardinecan/32/29141_2.png) [@sardinecan](https://discourse.julialang.org/u/sardinecan)
#### Post date: [December 21, 2022, 4:26pm UTC](https://discourse.julialang.org/t/send-file-with-http-post/91932/3 "2022-12-21T16:26:24Z")

</div>

@skleinbo thank you very much for your help, it works perfectly!!

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [December 21, 2022, 5:17pm UTC](https://discourse.julialang.org/t/send-file-with-http-post/91932/4 "2022-12-21T17:17:08Z")

</div>

That’s great! If you are happy with my answer, and feel like your problem is fully resolved, please mark it as the solution.

---

<div class="post-metadata">

### Author: ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)
#### Post date: [October 22, 2024, 12:39pm UTC](https://discourse.julialang.org/t/send-file-with-http-post/91932/5 "2024-10-22T12:39:13Z")

</div>

Here is another scenario as an example for other [Oxigraph](https://github.com/oxigraph/oxigraph) users. I wanted to translate the following curl command from the Oxigraph documentation into a call to `HTTP.post()`  
`curl -X POST -T my-turtle-file.ttl -H 'Content-Type: text/turtle' "http://127.0.0.1:7878/store?default"`  
This worked for me:

```julia
url = "http://127.0.0.1:7878/store?default"
headers = ["Content-Type" => "text/turtle"]
body=read("my-turtle-file", String)
HTTP.post(url, headers=headers, body=body)

```
