2

I'm trying to concatenate bit strings

 cowboy_req:reply(

               200, #{<<"content-type">> => <<"text/html">>},

               <<"<div style='color:#FF0'>">> ++ cowboy_req:host(Req) ++ <<"</div>">> , 

               Req
    )

but it gives runtime error because of ++ operator. How can I concatenate two bit strings?

Paramore
  • 1,313
  • 2
  • 19
  • 37
  • 1
    Please take a look at the following thread: http://stackoverflow.com/questions/10963359/concatenating-bitstrings-not-binaries-in-erlang – Yaron Jul 24 '16 at 06:07
  • I looked at that question but couldn't understand. There are some numbers like this `<<1,2>>` , but I have just strings in between `<< >>` – Paramore Jul 24 '16 at 06:12
  • What about this one? http://stackoverflow.com/a/601482/1835621 – Yaron Jul 24 '16 at 06:16

1 Answers1

6

What you have here are normal binaries, not specifically bitstrings.

If you really want to concatenate them, store cowboy_req:host(Req) in a variable and then concatenate the 3 binaries:

Host = cowboy_req:host(Req),
cowboy_req:reply(
    200,
    #{<<"content-type">> => <<"text/html">>},
    <<"<div style='color:#FF0'>", Host/binary, "</div>">>, 
    Req
)

Note that since cowboy_req:reply accepts iodata(), it's usually more efficient to return a list like this:

cowboy_req:reply(
    200,
    #{<<"content-type">> => <<"text/html">>},
    [<<"<div style='color:#FF0'>">>, cowboy_req:host(Req), <<"</div>">>], 
    Req
)
Dogbert
  • 212,659
  • 41
  • 396
  • 397