无法使用 Elixir 和 Hackney 将 pdf 上传到 slack "no_file_data"
Can't upload pdf to slack "no_file_data" with Elixir and Hackney
我正在用 Elixir 编写一个 slack 机器人,它必须将 pdf 上传到频道。
这是我想出的:
{:ok, ref} = :hackney.request(
:post,
"https://slack.com/api/files.upload?"
<> Plug.Conn.Query.encode(%{
"channels" => channel_id,
"filename" => "test.pdf",
"filetype" => "pdf"
}),
[
{"Content-Type", "multipart/form-data"},
{"Authorization", "Bearer xoxb-XXXXX"},
],
:stream_multipart,
[])
:hackney.send_multipart_body(
ref,
{:file, "./test.pdf"})
{:ok, _status, _headers, ref} = :hackney.start_response(ref)
{:ok, body} = :hackney.body(ref)
body |> Poison.decode! |> IO.inspect
但是我得到这个错误:
%{"error" => "no_file_data", "ok" => false}
我以这个问题为基础:
版本:
- 长生不老药:1.10.2
- 一次性密码:22.0.7
- 哈克尼:1.15
- PLug: 1.10
问题已解决。我只需要删除请求的 header 中的 {"Content-Type", "multipart/form-data"}
。哈克尼已经提供了一个 边界 .
我的 Content-Type
看起来像这样:
Content-Type: multipart/form-data
当它应该是这样的时候:
Content-Type: multipart/form-data; boundary=---------------------------tlzhgtmgeelwolfm
这是更正后的代码:
{:ok, ref} = :hackney.request(
:post,
"https://slack.com/api/files.upload?"
<> Plug.Conn.Query.encode(%{
"channels" => channel_id,
"filename" => "test.pdf",
"filetype" => "pdf"
}),
[
{"Authorization", "Bearer xoxb-XXXXX"},
],
:stream_multipart,
[])
:hackney.send_multipart_body(
ref,
{:file, "./test.pdf"})
{:ok, _status, _headers, ref} = :hackney.start_response(ref)
{:ok, body} = :hackney.body(ref)
body |> Poison.decode! |> IO.inspect
我正在用 Elixir 编写一个 slack 机器人,它必须将 pdf 上传到频道。
这是我想出的:
{:ok, ref} = :hackney.request(
:post,
"https://slack.com/api/files.upload?"
<> Plug.Conn.Query.encode(%{
"channels" => channel_id,
"filename" => "test.pdf",
"filetype" => "pdf"
}),
[
{"Content-Type", "multipart/form-data"},
{"Authorization", "Bearer xoxb-XXXXX"},
],
:stream_multipart,
[])
:hackney.send_multipart_body(
ref,
{:file, "./test.pdf"})
{:ok, _status, _headers, ref} = :hackney.start_response(ref)
{:ok, body} = :hackney.body(ref)
body |> Poison.decode! |> IO.inspect
但是我得到这个错误:
%{"error" => "no_file_data", "ok" => false}
我以这个问题为基础:
版本:
- 长生不老药:1.10.2
- 一次性密码:22.0.7
- 哈克尼:1.15
- PLug: 1.10
问题已解决。我只需要删除请求的 header 中的 {"Content-Type", "multipart/form-data"}
。哈克尼已经提供了一个 边界 .
我的 Content-Type
看起来像这样:
Content-Type: multipart/form-data
当它应该是这样的时候:
Content-Type: multipart/form-data; boundary=---------------------------tlzhgtmgeelwolfm
这是更正后的代码:
{:ok, ref} = :hackney.request(
:post,
"https://slack.com/api/files.upload?"
<> Plug.Conn.Query.encode(%{
"channels" => channel_id,
"filename" => "test.pdf",
"filetype" => "pdf"
}),
[
{"Authorization", "Bearer xoxb-XXXXX"},
],
:stream_multipart,
[])
:hackney.send_multipart_body(
ref,
{:file, "./test.pdf"})
{:ok, _status, _headers, ref} = :hackney.start_response(ref)
{:ok, body} = :hackney.body(ref)
body |> Poison.decode! |> IO.inspect