Reputation: 708
I am trying to fire a httpc request.
httpc:request(post, {Baseurl,[{"Authorization",ApiKey2}],<<"application/json">>,Body},[],[]).
But when i pass try to formulate Body Which is given below :-
Body = lists:flatten(mochijson2:encode({struct,[{"registration_ids",[{array, "APA91bE445JOlMvdShgQAJIxuDcBQos7olZi82lWQ82W9HHTR0uxOILvDYo9F5827BhU0qpBi9xpBqN1BNciWogiWxenI7_au7Z42o6eqcFSkoAs-0tVJzVG3lju54PXRyVS1tmZNhjsQDCYwnHJH6m6j6h1vSPBZl6lt3j8tu44Euq3PyISKBM"}]},
{"data",[{array,[{struct, [{"message","HGHJG"}]}]}]}
]})).
it gives me error like:- mod_gcm_apns:send_gcm_msg("GJHGHJ").
{error,{"HTTP/1.1",
"Field \"data\" must be a JSON array: [[{\"message\":[72,71,72,74,71]}]]\n"}}
tried a lot but not able to find a solution.
After making changes as suggested by mdaguete
% Create Json struct
Body = lists:flatten(mochijson2:encode(
{struct,[
{<<"registration_ids">>,[<<"APA91bE445JOlMvdShgQAJIxuDcBQos7olZi82lWQ82W9HHTR0uxOILvDYo9F5827BhU0qpBi9xpBqN1BNciWogiWxenI7_au7Z42o6eqcFSkoAs-0tVJzVG3lju54PXRyVS1tmZNhjsQDCYwnHJH6m6j6h1vSPBZl6lt3j8tu44Euq3PyISKBM">>]},
{<<"data">>,[
{struct, [
{<<"message">>,<<"HGHJG">>}
]}
]}
]})),
execution line:-
mod_gcm2:send_gcm_msg("JKJ").
But it is giving following error:
{error,{"HTTP/1.1",
"JSON_PARSING_ERROR: Unexpected token END OF FILE at position 27.\n"}}
Kindly suggest a solution.
Upvotes: 1
Views: 215
Reputation: 708
Body Variable:-
Body = lists:flatten(mochijson2:encode(
{ struct,[
{ registration_ids,
['KEY']},
{ data,[{
message,[Message]
}
This is the final JSON Packet that worked with Google GCM API.
Upvotes: 1
Reputation: 387
Usually the best and fastest way to solve a problem is reading the doc.
You're using the excelent mochijson2 library from Bob Ippolito, if you read the source code header you can read the correct format to express json in erlang terms.
The correct format for your json could be (you didn't explain the result expected):
{struct,[
{<<"registration_ids">>,[<<"APA91bE445JOlMvdShgQAJIxuDcBQos7olZi82lWQ82W9HHTR0uxOILvDYo9F5827BhU0qpBi9xpBqN1BNciWogiWxenI7_au7Z42o6eqcFSkoAs-0tVJzVG3lju54PXRyVS1tmZNhjsQDCYwnHJH6m6j6h1vSPBZl6lt3j8tu44Euq3PyISKBM">>]},
{<<"data">>,[
{struct, [
{<<"message">>,<<"HGHJG">>}
]}
]}
]}
Regards.
Upvotes: 1