2016-08-01 7 views
0

POST要求をJSONオブジェクトを表す文字列で取得します。Erlang内のPOST要求のアクセス・ボディhttpd

_Envリストは、次のようになります。サーバを起動する

[ 
    {server_software,"inets/6.3"}, 
    {server_name,"XTR20160414"}, 
    {gateway_interface,"CGI/1.1"}, 
    {server_protocol,"HTTP/1.1"}, 
    {server_port,9000}, 
    {request_method,"POST"}, 
    {remote_addr,"127.0.0.1"}, 
    {peer_cert,undefined}, 
    {script_name,"/api/server:auth"}, 
    {http_host,"localhost:9000"}, 
    {http_accept,"application/json"}, 
    {http_content_type,"application/json"}, 
    {http_content_length,"34"}, 
    {http_connection,"close"}, 
    {content_length,34} 
] 

私のコードは次のとおりです。

http://localhost:9000/api/server:auth 

start() -> 
    mnesia:start(), 
    inets:start(httpd, [ 
     {modules, [ 
      mod_alias, 
      mod_auth, 
      mod_esi, 
      mod_actions, 
      mod_cgi, 
      mod_dir, 
      mod_get, 
      mod_head, 
      mod_log, 
      mod_disk_log 
     ]}, 
     {port, 9000}, 
     {server_name, "pokerspace"}, 
     {server_root, "misc/log"}, 
     {document_root, "misc/www"}, 
     {erl_script_alias, {"/api", [server]}}, 
     {error_log, "error.log"}, 
     {security_log, "security.log"}, 
     {transfer_log, "transfer.log"}, 
     {mime_types, [ 
      {"json", "application/json"}, 
      {"html", "text/html"}, 
      {"css", "text/css"}, 
      {"js", "application/x-javascript"} 
     ]} 
    ]). 

その後、私は内部のいくつかのデータでこのURLへのPOSTリクエストを行います

このリクエストをこのコードで処理します:

auth(SessionID, _Env, _Input) -> 
    mod_esi:deliver(SessionID, [ 
     text_header(), 
     "authenticated" 
    ]). 

私はcontent_lengthは、データが入って来ているように見えるので、適切な数に見えますこれ、34であることがわかります。今

、どのように私はそれで何かを行くためにポストされたデータを抽出していますか?

+0

あなたはどのように/あなたがこのENVリストを得たところから上のいくつかのコードを追加していただけますか? – Dogbert

+0

質問に追加した詳細をご覧ください。 –

答えて

1

POST本体は、mod_esi docsで説明されているように、3番目の引数としてコールバック関数に渡されます。

これは、自分自身と連結体に渡さ返します。

auth(SessionID, _Env, Input) -> 
    mod_esi:deliver(SessionID, [ 
     Input, Input 
    ]). 

デモ:

$ curl -XPOST -d "hi" http://localhost:9000/api/server:auth 
hihi 
+0

私はそう思っていましたが、初めて試してみました。何らかの理由でうまくいかず、小さなものがあるかもしれません。ありがとう! –

関連する問題