简体   繁体   中英

Concat 2 strings erlang and send with http

I'm trying to concat 2 variables Address and Payload. After that I want to send them with http to a server but I have 2 problems. When i try to concat the 2 variables with a delimiter ';' it doesn't work. Also sending the data of Payload or Address doesn't work. This is my code:

handle_rx(Gateway, #link{devaddr=DevAddr}=Link, #rxdata{port=Port, data= RxData }, RxQ)->
    Data = base64:encode(RxData),
    Devaddr = base64:encode(DevAddr),
    TextAddr="Device address: ",
    TextPayload="Payload: ",
    Address = string:concat(TextAddr, Devaddr),
    Payload = string:concat(TextPayload, Data),
    Json=string:join([Address,Payload], "; "),
    file:write_file("/tmp/foo.txt", io_lib:fwrite("~s.\n", [Json] )),
    inets:start(),
    ssl:start(),
    httpc:request(post, {"http://192.168.0.121/apiv1/lorapacket/rx", [], "application/x-www-form-urlencoded", Address },[],[]),
    ok;
handle_rx(_Gateway, _Link, RxData, _RxQ) ->
    {error, {unexpected_data, RxData}}.

I have no errors that I can show you. When I write Address or Payload individually to the file it works but sending doesn't work...

Thank you for your help!

When i try to concat the 2 variables with a delimiter ';' it doesn't work.

5> string:join(["hello", <<"world">>], ";").
[104,101,108,108,111,59|<<"world">>]

6> string:join(["hello", "world"], ";").    
"hello;world"

base64:encode() returns a binary, yet string:join() requires string arguments. You can do this:

7> string:join(["hello", binary_to_list(<<"world">>)], ";").
"hello;world"

Response to comment :

In erlang the string "abc" is equivalent to the list [97,98,99] . However, the binary syntax <<"abc">> is not equivalent to <<[97,98,99]>> , rather the binary syntax <<"abc">> is special short hand notation for the binary <<97, 98, 99>> .

Therefore, if you write:

Address = [97,98,99].

then the code:

Bin = <<Address>>.

after variable substitution becomes:

Bin = <<[97,98,99]>>.

and that isn't legal binary syntax.

If you need to convert a string/list contained in a variable, like Address, to a binary, you use list_to_binary(Address) --not <<Address>> .

In your code here:

Json = string:join([binary_to_list(<<Address>>),
                    binary_to_list(<<Pa‌​yload>>)], 
                    ";").

Address and Payload were previously assigned the return value of string:concat() , which returns a string, so there is no reason to (attempt) to convert Address to a binary with <<Address>> , then immediately convert the binary back to a string with binary_to_list() . Instead, you would just write:

Json = string:join(Address, Payload, ";")

The problem with your original code is that you called string:concat() with a string as the first argument and a binary as the second argument--yet string:concat() takes two string arguments. You can use binary_to_list() to convert a binary to the string that you need for the second argument.

Sorry I'm new to Erlang

As with any language, you have to study the basics and write numerous toy examples before you can start writing code that actually does something.

You don't have to concatenate strings. It is called iolist and is one of best things in Erlang:

1> RxData = "Hello World!", DevAddr = "Earth",
1> Data = base64:encode(RxData), Devaddr = base64:encode(DevAddr),
1> TextAddr="Device address", TextPayload="Payload",
1> Json=["{'", TextAddr, "': '", Devaddr, "', '", TextPayload, "': '", Data, "'}"].
["{'","Device address","': '",<<"RWFydGg=">>,"', '",
 "Payload","': '",<<"SGVsbG8gV29ybGQh">>,"'}"]
2> file:write_file("/tmp/foo.txt", Json).
ok
3> file:read_file("/tmp/foo.txt").
{ok,<<"{'Device address': 'RWFydGg=', 'Payload': 'SGVsbG8gV29ybGQh'}">>}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM