내가 가진 Mochiweb하는 매우 간단한 '안녕하세요'Mochiweb 서버 (난 그냥에 내 소개를 시작)하는 JSON 요청을 받아 다시 보내는 :간단한 JSON 요청
'POST' ->
case Path of
"dummy" ->
Data = Req:parse_post(),
Json = proplists:get_value("json", Data),
Struct = mochijson2:decode(Json),
Action_value = struct:get_value(<<"action">>, Struct),
Action = list_to_atom(binary_to_list(A)),
Result = [got_json_request, Action],
DataOut = mochijson2:encode(Result),
Req:ok({"application/json",[],[Result]});
것은
curl -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"action":"dummy"}' http://localhost:8080/dummy
Mochiweb 로그 읽기가 매우 어렵습니다,하지만 난 이해 오류가이 라인에서 발생합니다 :
Json = proplists:get_value("json", Data)
나는 컬로로 요청을하려고 할 때 실패한다는 것입니다
코드에 io:format
두 개를 넣었고 변수가 []
인 경우 Data
과 Json
이 모두 cURL로 요청한 후 발견되었습니다. 한편
curl -d '{"action":"dummy"}' http://localhost:8080/dummy
데이터 및 JSON 모두 [{"{\"action\":\"dummy\"}",[]}]
하지만,이 경우 라인 Struct = mochijson2:decode(Json)
이 실패합니다.
일부 이상한 이유로 Mochiweb은 헤더에 "application/json"값이있는 경우 POST 요청에서 JSON 데이터를 보지 않습니다.
그래서 질문입니다 : JSON 데이터를 사용하여 Mochiweb 서버에 올바른 POST 요청을하는 방법은 무엇입니까?
편집 : Json
변수의 값은 undefined
입니다.
Data = Req:recv_body(),
Json = mochijson2:decode(Data),
...
의 라인을 따라
관련이 없지만 list_to_atom은 제한적이라는 보장없이 취할 매우 위험한 전술입니다. 매우 큰 경우에도 특정한 얼랑 인스턴스에 존재할 수있는 유한의 원자 수가 있습니다. – Greyson
원자는 가비지 수집되지 않으므로 응용 프로그램의 수명 동안 존재합니다. –