I just completed invoking SOAP Webservice from Erlang.
Here is what I did,
- inets:start(), This is very important without which http:request will not be succeeded.
- Took a shortcut: (hm.. not actually) I treated SOAP Call as Http POST call.
- I used Erlang http module
- http:request( post, URL, [{"Host" , Host}, {"SOAPAction", ""}],
"text/xml", getData ()},
[ {timeout, 5000} ], [{body_format, binary}])
- Used case expression to handle different condition
- getData() returns binary form of soap message. Here, correct payload of SOAP Request will be needed to make this request template. It returns binary form of the SOAP payload (String form can also be used)
- http:request would return either
- {ok, Result} -> Handle successful http request
- {error, Reason} -> Handle Errors
- {ok, saved_to_file}-> if you use “stream” option, this pattern will be invoked and it is optional.
- Result is response payload (http payload) and can be processed
Note:
Erlang has good support for Proxy and Proxy authentication unlike python. Python is mess in this area.
please, can you post a full example? maybe using a public web service?
ReplyDeletethanks in advance
http://www.redaelli.org/matteo/
Thanks for contacting me.... I used plain http to make POST request.
ReplyDeleteHere is the code to lat lang service,
call
soap:lang_lat_service("ZIP_CODE").
Sorry for the bad indentation.
-module(soap).
-export([lang_lat_service/1]).
-define(URL,"http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLserver.php"\
).
-define(HOST,"http://www.weather.gov").
lang_lat_service(ZipCode) ->
io:format("Data is ~p~n", [ZipCode]),
RequestPayload = getData(ZipCode),
io:format("Data is ~p~n", [RequestPayload]),
make_call(RequestPayload),
ok.
make_call(Data)->
inets:start(),
{ok, Result} = http:request( post, {?URL, [{"Host" , ?HOST}, {"SOAPAction", \
"http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCod\
e"}], "text/xml", Data}, [ {timeout, 5000} ], [{body_format, binary}]), io:fo\
rmat("Soap Response is ~p~n", [Result]),
Result.
process_request(Result)->
%% case {ok, Result} ->
%% io:format("Soap Response is ~p~n", [Result]);
%% {error, Reason} ->
%% io:format("Error is ~p~n", [Reason])
%% end.
ok.
getData(ZipCode)->
XmlData = " <soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-i\
nstance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://\
schemas.xmlsoap.org/soap/envelope/\" xmlns:ndf=\"http://www.weather.gov/forecast\
s/xml/DWMLgen/wsdl/ndfdXML.wsdl\"> <soapenv:Header/> <soapenv:Body> <nd\
f:LatLonListZipCode soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/enco\
ding/\"> <zipCodeList xsi:type=\"dwml:zipCodeListType\" xmlns:dwml=\"htt\
p://www.weather.gov/forecasts/xml/DWMLgen/schema/DWML.xsd\">"
++ ZipCode
++ "</zipCodeList> </ndf:LatLonListZipCode> </soapenv:Body></soap\
env:Envelope>"
Hi Sethilkumar,
ReplyDeleteThanks for the codes. It helps me a lot.
:)
Abe
Abe, you welcome....
ReplyDeleteAbe, better way to handle web service is to use XML parsing capabilities available in Erlang but I wanted to show how to use raw http api to invoke web service as it is "glorified" http designed by committee ;-)
ReplyDelete