如何创建 JSON 对象并在 REST API 调用中传递它?
How to create a JSON Object and pass it in a REST API call?
我是 Erlang 的新手,我的疑问是如何在 Erlang 中创建一个 JSON 对象并在 REST API 调用中传递该对象。看了那么多帖子都没有满意的答案
编辑
我在这里调用 API:
offline_message(From, To, #message{type = Type, body = Body}) ->
Type = xml:get_text(Type),
Body = xml:get_text(Body),
Token = gen_mod:get_module_opt(To#jid.lserver, ?MODULE, auth_token, fun(S) -> iolist_to_binary(S) end, list_to_binary("")),
PostUrl = gen_mod:get_module_opt(To#jid.lserver, ?MODULE, post_url, fun(S) -> iolist_to_binary(S) end, list_to_binary("")),
to = To#jid.luser
from = From#jid.luser
if
(Type == <<"chat">>) and (Body /= <<"">>) ->
Sep = "&",
Post = {
"token":binary_to_list(Token),
"from":binary_to_list(from),
"to":binary_to_list(to),
"body":binary_to_list(Body)
},
?INFO_MSG("Sending post request to ~s with body \"~s\"", [PostUrl, Post]),
httpc:request(post, {binary_to_list(PostUrl), [], "application/json", binary_to_list(Post)},[],[]),
ok;
true ->
ok
end.
这里关于 JSON 字符串的一切都正常吗?我正在尝试修改此 module.
How to create a JSON Object in Erlang
erlang 中没有 objects 这样的东西,所以简单的回答是:你不能。但是,您通过电线发送的内容只是字符串,您当然可以使用 erlang 创建字符串。
为了使事情更简单,您可以使用像 jsx to create the json formatted strings that you want to send in your request. However, in order to use erlang modules you will have to learn a little about rebar3
, which is erlang's package installer (see What is the easiest way for beginners to install a module?) 这样的 erlang 模块。
请记住,http 请求只是一个以特定方式格式化的字符串。一个 http 请求以这样一行开头:
POST /some/path HTTP/1.1
然后有几行文本叫做headers,看起来像:
User-Agent: Mozilla-yah-yah-bah
Content-Type: application/json
Content-Length: 103
然后是几个换行符,后面是一些附加文本,称为 post body, 可以有几种不同的格式(格式应在Content-Type
header中声明):
Format Content-Type
------ -----------
"x=1&y=2" application/x-www-form-urlencoded
"{x:1, y:2}" application/json
"more complex string" multipart/form-data
为了更容易 assemble 一个 http 请求并将其发送到服务器,erlang 有一个名为 inets
的内置 http 客户端,你可以在文档中阅读 here. For an example that uses inets
, see here. Because inets
is a bit cumbersome to use, alternatively you can use a third party http client like hackney。不过,您需要再次安装 hackney
和 rebar3
。
一旦您发送了请求,服务器就会破译请求并采取必要的行动。
我是 Erlang 的新手,我的疑问是如何在 Erlang 中创建一个 JSON 对象并在 REST API 调用中传递该对象。看了那么多帖子都没有满意的答案
编辑
我在这里调用 API:
offline_message(From, To, #message{type = Type, body = Body}) ->
Type = xml:get_text(Type),
Body = xml:get_text(Body),
Token = gen_mod:get_module_opt(To#jid.lserver, ?MODULE, auth_token, fun(S) -> iolist_to_binary(S) end, list_to_binary("")),
PostUrl = gen_mod:get_module_opt(To#jid.lserver, ?MODULE, post_url, fun(S) -> iolist_to_binary(S) end, list_to_binary("")),
to = To#jid.luser
from = From#jid.luser
if
(Type == <<"chat">>) and (Body /= <<"">>) ->
Sep = "&",
Post = {
"token":binary_to_list(Token),
"from":binary_to_list(from),
"to":binary_to_list(to),
"body":binary_to_list(Body)
},
?INFO_MSG("Sending post request to ~s with body \"~s\"", [PostUrl, Post]),
httpc:request(post, {binary_to_list(PostUrl), [], "application/json", binary_to_list(Post)},[],[]),
ok;
true ->
ok
end.
这里关于 JSON 字符串的一切都正常吗?我正在尝试修改此 module.
How to create a JSON Object in Erlang
erlang 中没有 objects 这样的东西,所以简单的回答是:你不能。但是,您通过电线发送的内容只是字符串,您当然可以使用 erlang 创建字符串。
为了使事情更简单,您可以使用像 jsx to create the json formatted strings that you want to send in your request. However, in order to use erlang modules you will have to learn a little about rebar3
, which is erlang's package installer (see What is the easiest way for beginners to install a module?) 这样的 erlang 模块。
请记住,http 请求只是一个以特定方式格式化的字符串。一个 http 请求以这样一行开头:
POST /some/path HTTP/1.1
然后有几行文本叫做headers,看起来像:
User-Agent: Mozilla-yah-yah-bah
Content-Type: application/json
Content-Length: 103
然后是几个换行符,后面是一些附加文本,称为 post body, 可以有几种不同的格式(格式应在Content-Type
header中声明):
Format Content-Type
------ -----------
"x=1&y=2" application/x-www-form-urlencoded
"{x:1, y:2}" application/json
"more complex string" multipart/form-data
为了更容易 assemble 一个 http 请求并将其发送到服务器,erlang 有一个名为 inets
的内置 http 客户端,你可以在文档中阅读 here. For an example that uses inets
, see here. Because inets
is a bit cumbersome to use, alternatively you can use a third party http client like hackney。不过,您需要再次安装 hackney
和 rebar3
。
一旦您发送了请求,服务器就会破译请求并采取必要的行动。