我正在尝试触发httpc请求。
httpc:request(post, {Baseurl,[{"Authorization",ApiKey2}],<<"application/json">>,Body},[],[]).
但是当我通过尝试制定下面给出的身体时: -
Body = lists:flatten(mochijson2:encode({struct,[{"registration_ids",[{array, "APA91bE445JOlMvdShgQAJIxuDcBQos7olZi82lWQ82W9HHTR0uxOILvDYo9F5827BhU0qpBi9xpBqN1BNciWogiWxenI7_au7Z42o6eqcFSkoAs-0tVJzVG3lju54PXRyVS1tmZNhjsQDCYwnHJH6m6j6h1vSPBZl6lt3j8tu44Euq3PyISKBM"}]},
{"data",[{array,[{struct, [{"message","HGHJG"}]}]}]}
]})).
它给我的错误如下: - mod_gcm_apns:send_gcm_msg(&#34; GJHGHJ&#34)
{error,{"HTTP/1.1",
"Field \"data\" must be a JSON array: [[{\"message\":[72,71,72,74,71]}]]\n"}}
尝试了很多,但无法找到解决方案。
根据mdaguete的建议进行更改
% Create Json struct
Body = lists:flatten(mochijson2:encode(
{struct,[
{<<"registration_ids">>,[<<"APA91bE445JOlMvdShgQAJIxuDcBQos7olZi82lWQ82W9HHTR0uxOILvDYo9F5827BhU0qpBi9xpBqN1BNciWogiWxenI7_au7Z42o6eqcFSkoAs-0tVJzVG3lju54PXRyVS1tmZNhjsQDCYwnHJH6m6j6h1vSPBZl6lt3j8tu44Euq3PyISKBM">>]},
{<<"data">>,[
{struct, [
{<<"message">>,<<"HGHJG">>}
]}
]}
]})),
执行行: -
mod_gcm2:send_gcm_msg("JKJ").
但是它给出了以下错误:
{error,{"HTTP/1.1",
"JSON_PARSING_ERROR: Unexpected token END OF FILE at position 27.\n"}}
请提出解决方案。
答案 0 :(得分:1)
通常,解决问题的最佳和最快方法是阅读文档。
您正在使用来自Bob Ippolito的优秀mochijson2库,如果您read the source代码标题,您可以阅读正确的格式以在erlang术语中表达json。
你的json的正确格式可能是(你没有解释预期的结果):
{struct,[
{<<"registration_ids">>,[<<"APA91bE445JOlMvdShgQAJIxuDcBQos7olZi82lWQ82W9HHTR0uxOILvDYo9F5827BhU0qpBi9xpBqN1BNciWogiWxenI7_au7Z42o6eqcFSkoAs-0tVJzVG3lju54PXRyVS1tmZNhjsQDCYwnHJH6m6j6h1vSPBZl6lt3j8tu44Euq3PyISKBM">>]},
{<<"data">>,[
{struct, [
{<<"message">>,<<"HGHJG">>}
]}
]}
]}
问候。
答案 1 :(得分:1)
身体变量: -
Body = lists:flatten(mochijson2:encode(
{ struct,[
{ registration_ids,
['KEY']},
{ data,[{
message,[Message]
}
这是与Google GCM API配合使用的最终JSON数据包。