# Uploading Multi-part files in julia

**URL:** <https://discourse.julialang.org/t/uploading-multi-part-files-in-julia/54200>\
**Category:** General Usage\
**Tags:** question, http\
**Created:** [January 29, 2021, 5:53pm UTC](https://discourse.julialang.org/t/uploading-multi-part-files-in-julia/54200 "2021-01-29T17:53:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dsantra92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dsantra92/32/21432_2.png) [@Dsantra92](https://discourse.julialang.org/u/Dsantra92)\
**Post date:** [January 29, 2021, 5:53pm UTC](https://discourse.julialang.org/t/uploading-multi-part-files-in-julia/54200/1 "2021-01-29T17:53:44Z")

</div>

I am trying to upload an image to Slack using their API.

I have successfully uploaded some text message using the following function :

```julia
function send_message(msg::T, channel_id::T, token::T) where T<:AbstractString
    msg = Dict("channel"=>channel_id, "text"=>"$(msg)")
    msg_payload = JSON.json(msg)
    token = "Bearer " * token
    params = ["Content-Type" => "application/json; charset=utf-8", "Authorization" => token]
    url = "https://slack.com/api/chat.postMessage"

    r = HTTP.request("POST", url, params, msg_payload)
    return r
end

```

But I am failing to do the same for files, I have to do something with `HTTP.form` or `HTTP.multipart` but I could not find those documentation.

Here is the code snippet I am trying to upload a file :

```julia
channel_id = "C01HJGL1MTK"
auth_key = "xoxb-1590094798692-1596646938673-3PC9eIaEa8ojPiEuzL6nJBM6"
key = "Bearer "*auth_key
url = "https://slack.com/api/files.upload"

file = open("tmp/plot2.jpg", "r")
part_file = HTTP.Form(Dict("file"=>file))
params = ["Content-Type" => "multipart/form-data", "Authorization" => key]
payload = HTTP.Form(Dict("channel"=>channel_id, "file"=>file))  
# Error in the above line, but I am getting a workaround 

HTTP.request("POST", url, params, payload)

```

Provided the channel key and bearer token if anyone wanted to reproduce.

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [February 18, 2021, 1:29pm UTC](https://discourse.julialang.org/t/uploading-multi-part-files-in-julia/54200/2 "2021-02-18T13:29:34Z")

</div>

hey, did you solve this? thanks

---

<div class="post-metadata">

**Author:** ![Dsantra92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dsantra92/32/21432_2.png) [@Dsantra92](https://discourse.julialang.org/u/Dsantra92)\
**Post date:** [February 21, 2021, 1:41pm UTC](https://discourse.julialang.org/t/uploading-multi-part-files-in-julia/54200/3 "2021-02-21T13:41:53Z")

</div>

I haven’t got a pure julia solution yet. I am just calling up python to deal with it until I find one.

---

<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:** [February 21, 2021, 3:43pm UTC](https://discourse.julialang.org/t/uploading-multi-part-files-in-julia/54200/4 "2021-02-21T15:43:02Z")

</div>

I tried to improve the docs for `HTTP.Form` and `HTTP.Multipart` here: [HTTP.jl#676](https://github.com/JuliaWeb/HTTP.jl/pull/676)

I believe (part of) the problem is that your `Content-Type` header does not include the multipart boundary. Fortunately [HTTP.jl should add that automatically](https://github.com/JuliaWeb/HTTP.jl/pull/473) if your body is `HTTP.Form`.

This should probably work, but I have not tested with the Slack API:

```julia
headers = ["Authorization" => "Bearer *****"]
data = ["channel" => " *****", "file" => open("tmp/plot2.jpg", "r")]
body = HTTP.Form(data)
HTTP.post(url, headers, body)

```

---

<div class="post-metadata">

**Author:** ![Dsantra92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dsantra92/32/21432_2.png) [@Dsantra92](https://discourse.julialang.org/u/Dsantra92)\
**Post date:** [February 22, 2021, 12:08pm UTC](https://discourse.julialang.org/t/uploading-multi-part-files-in-julia/54200/5 "2021-02-22T12:08:56Z")

</div>

This really helped me. Thank you!!! Can we add some examples of uploading files in doc??

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [February 24, 2021, 10:25pm UTC](https://discourse.julialang.org/t/uploading-multi-part-files-in-julia/54200/6 "2021-02-24T22:25:12Z")

</div>

that’s clearly the proper solution. I am just calling the file upload API via `curl` and that also works for me (not sure that is what you are after…):

```julia
Base.run(`curl -F file=@$(fname) -F "initial_comment=say what" -F channels=xxxxxxxxx -H "Authorization: Bearer $(ENV["SLACK_FILES"])" https://slack.com/api/files.upload`)

```
