Search code examples
erlangejabberd

httpc request: Bad argument exception length


I´ve a problem to make a httpc request to my Java servlet. On my local system, the code is working fine, but if I execute the call from an erlang file of the ejabberd server, I get a bad argument exception.

This is my code from my local system:

Body = "deviceToken=dy....[Very long FCM token]....b1&sender=epeued7o3z", inets:start(), httpc:request(post, { "http://192.168.2.110:8080/020/service", [], "application/x-www-form-urlencoded", Body }, [], []).

I copied the code to the erlang file of the ejabberd server and I got this exception:

2017-11-24 18:05:40 =CRASH REPORT==== crasher: initial call: httpc_handler:init/1 pid: <0.522.0> registered_name: [] exception error: bad argument: [{erlang,length,[[100,101,118,105,99,101,84,111,107,101,110,61,<<"dy....[Very long FCM token]...Kn4b1">>,38,115,101,110,100,101,114,61|<<"epeued7o3z">>]],[]},{httpc_request,body_length,1,[{file,"httpc_request.erl"},{line,233}]},{httpc_request,post_data,4,[{file,"httpc_request.erl"},{line,208}]},{httpc_request,send,4,[{file,"httpc_request.erl"},{line,85}]},{httpc_handler,connect_and_send_first_request,3,[{file,"httpc_handler.erl"},{line,815}]},{httpc_handler,init,1,[{file,"httpc_handler.erl"},{line,238}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}] ancestors: [httpc_handler_sup,httpc_sup,inets_sup,<0.439.0>] message_queue_len: 0 messages: [] links: [<0.445.0>,#Port<0.14269>] dictionary: [] trap_exit: true status: running heap_size: 610 stack_size: 27 reductions: 978 neighbours: 2017-11-24 18:05:40 =SUPERVISOR REPORT==== Supervisor: {local,httpc_handler_sup} Context: child_terminated Reason: {badarg,[{erlang,length,[[100,101,118,105,99,101,84,111,107,101,110,61,<<"dy...[Very long FCM token].....Kn4b1">>,38,115,101,110,100,101,114,61|<<"epeued7o3z">>]],[]},{httpc_request,body_length,1,[{file,"httpc_request.erl"},{line,233}]},{httpc_request,post_data,4,[{file,"httpc_request.erl"},{line,208}]},{httpc_request,send,4,[{file,"httpc_request.erl"},{line,85}]},{httpc_handler,connect_and_send_first_request,3,[{file,"httpc_handler.erl"},{line,815}]},{httpc_handler,init,1,[{file,"httpc_handler.erl"},{line,238}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]} Offender: [{pid,<0.522.0>},{id,undefined},{mfargs,{httpc_handler,start_link,undefined}},{restart_type,temporary},{shutdown,4000},{child_type,worker}]

Is it right that the body length is wrong / to long, because on my local program it is working well? The request to the servlet should not be the problem.

Thanks in advance!


Solution

  • exception error: bad argument:
    [{erlang,length,[
        [100,101,118,105,99,101,84,111,107,101,110,61,
         <<"dy....[Very long FCM token]...Kn4b1">>,
         38,115,101,110,100,101,114,61|<<"epeued7o3z">>]
        ]                             ^
                                      |
     This create an improper list that make the function length/1 fail.
    

    You have defined an improper list, because the last term, after the cons operator: <<"epeued7o3z">> is a binary, not a list.

    You can solve this by writting:

    [100,101,118,105,99,101,84,111,107,101,110,61,
     <<"dy....[Very long FCM token]...Kn4b1">>,
     38,115,101,110,100,101,114,61,
     <<"epeued7o3z">>
    ]
    

    or

    [100,101,118,105,99,101,84,111,107,101,110,61,
     <<"dy....[Very long FCM token]...Kn4b1">>,
     38,115,101,110,100,101,114,61|"epeued7o3z"
    ]
    

    The call to length/1 will return the length of the iolist, note that the lengths are different, the first list length is 22, while the second one is 31.

    If you need a flat list rather than an iolist, I don't know any function that make it directly, but you can convert the iolist to a binary first, and then convert the binary into a list (and the length is now 65):

    1> IoList = [100,101,118,105,99,101,84,111,107,101,110,61,<<"dy....[Very long FCM token]...Kn4b1">>,38,115,101,110,100,101,114,61,<<"epeued7o3z">>].
    [100,101,118,105,99,101,84,111,107,101,110,61,
     <<"dy....[Very long FCM token]...Kn4b1">>,38,115,101,110,
     100,101,114,61,<<"epeued7o3z">>]
    2> binary_to_list(iolist_to_binary(IoList)).
    "deviceToken=dy....[Very long FCM token]...Kn4b1&sender=epeued7o3z"