通過 erlang 向伺服器發送 http post 請求

來源:互聯網
上載者:User

標籤:

 1 -module(t).   2    3 -export([test/0]).   4    5 test()->   6     Method = post, 7     URL = "http://192,168.124.10:3000/node/2.0/new", 8     Header = [], 9     Type = "application/json",10     Body = "{\"name\":\"foo.example.com\"}",11     HTTPOptions = [],12     Options = [],13     case httpc:request(Method, {URL, Header, Type, Body}, HTTPOptions, Options) of14         {ok, {_,_,Body}}-> 15             io:format("receive body:~p~n", [Body]),16             {"id", Key} = lists:keyfind("id",1,json:parse(Body)),17             io:format("Key is:~p~n", [Key]);18         {error, Reason}->19             io:format("error cause ~p~n",[Reason])  20     end.

通過文檔可以瞭解到httpc:request/4的使用方法。如果request方法的參數填對就可以得到{ok,Result}, Result -> 

{status_line(), headers(), Body} | {status_code(), Body} | request_id() },

這裡得到的Result 為 {status_line(), headers(), Body},故上面是只取了需要的Body。即Boby為請求的傳回值。

這裡要注意的是Body的傳回值是否符合期望,關聯的是request 所提交的Url,索引值等等內容是否和後端達成交

互,true -> 期望值;false -> [ ]。

通過 erlang 向伺服器發送 http post 請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.