rest - erlang response http request in decimal instead of letter -
i'm beginning using erlang cowboy , leptus create rest api.
so tried simple:
myapp_handler.erl
-module(myapp_handler). -compile({parse_transform, leptus_pt}). -export([init/3]). -export([cross_domains/3]). -export([terminate/4]). -export([post/3]). %% cross domain origin %% accepts host cross-domain requests cross_domains(_route, _req, state) -> {['_'], state}. %% start init(_route, _req, state) -> {ok, state}. post("/myrequest", req, state) -> json = "[ { \"test\": \"hello\" } ]", {200, {json, json}, state}. %% end terminate(_reason, _route, _req, _state) -> ok.
after launching server, tried run post request through curl:
curl -x post http://127.0.0.1:8080/myrequest --header "content-type:text/json"
and answer of request is:
[91,10,32,32,32,32,123,10,32,32,32,32,32,32,34,116,101,115,116,34,58,32,34,104,101,108,108,111,34,10,32,32,32,32,125,10,32,32,93]
all numbers value of character in decimal ascii table. wonder why answer of request displayed in numbers instead of letters. did wrong?
thank in advance
i haven't used leptus before, but, let's take @ example rest endpoint readme on github (https://github.com/s1n4/leptus):
get("/", _req, state) -> {<<"hello, leptus!">>, state}; get("/hi/:name", req, state) -> status = ok, name = leptus_req:param(req, name), body = [{<<"say">>, <<"hi">>}, {<<"to">>, name}], {status, {json, body}, state}.
it appears if post tuple of {json, term}
automatically convert erlang term json. instead of making json manually instead use:
post("/myrequest", req, state) -> json = [{<<"test">>, <<"hello">>}], {200, {json, json}, state}.
it appears leptus expecting strings , json keys , json string values passed in binaries. if wanted return simple string of output use following:
post("/myrequest", req, state) -> {200, <<"hello">>, state}.
typically libraries use binaries instead of standard erlang strings because binaries more efficient erlang string lists.