我目前正在 Erlang 中开发一个模块,该模块发送一个 HTTP POST 请求,其中包含包含 JSON 和二进制数据的多部分/相关消息。但是,我在正确连接 JSON 消息和二进制数据时遇到问题。
-module(MIME_post).
-export([send_post_request/0, get_modify_req/0]).
send_post_request() ->
% ... (existing code)
Url = "http://localhost:8666",
JsonData = #{<<"some_val">> => <<"imsi-460886666660006">>},
JsonBody = jsx:encode(JsonData),
Headers = [{"Content-Type", "multipart/related;boundary=-21ba7406cff5fa6c192d6b78fe58e16c5fd0cde4111e10f11c2bf55b45a5"}],
MimeBinary = <<"2e0a00d1">>,
Request = {Url, Headers, "multipart/related", JsonBody},
%here i want to include that MIME binary the Json body should get recognised as json and MIME binary should get recognized as another content type
case httpc:request(post, Request, [stream], []) of
{ok, {{_, 201, _}, _Headers, ResponseBody}} ->
io:format("HTTP Response Header:~p~n", [Headers]),
io:format("HTTP Response: ~s~n", [ResponseBody]),
{ok, ResponseBody};
{error, Reason} ->
io:format("HTTP Request failed with reason: ~p~n", [Reason]),
{error, Reason}
end.
Headers = [{"Content-Type",
“多部分/相关;边界=-21ba7406cff5fa6c192d6b78fe58e16c5fd0cde4111e10f11c2bf55b45a5”}],
改变:
-21ba7406cff5fa6c192d6b78fe58e16c5fd0cde4111e10f11c2bf55b45a5
至:
21ba7406cff5fa6c192d6b78fe58e16c5fd0cde4111e10f11c2bf55b45a5
另外,补充一下:
;type=\"application/json\"
到最后。必填。
您还需要指定一个
Content-Length
标头以及正文中的确切字节数。
那么,这就是您想要的身体样子:
--21ba7406cff5fa6c192d6b78fe58e16c5fd0cde4111e10f11c2bf55b45a5
Content-Type: application/json <followed by two newlines>
<JSON HERE>
--21ba7406cff5fa6c192d6b78fe58e16c5fd0cde4111e10f11c2bf55b45a5
Content-Type: application/octet-stream <followed by two newlines>
<BINARY HERE>
--21ba7406cff5fa6c192d6b78fe58e16c5fd0cde4111e10f11c2bf55b45a5
将
<JASON HERE>
替换为您的 json,将 <BINARY HERE>
替换为您的二进制数据,并将 <followed by two newlines>
替换为两个换行符。
祝你好运。