Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
When the user sends a POST request to the web server, that is the
key to create a new airport record. The handler needs to find the airport
name and other information from the POST content with yaws_api:postvar/2, and then should create a new
airport with airport:create_airport/5.
Example 4-12 takes the airport name and other
information, creates an airport record, and inserts it into the Mnesia
database. The nice thing about Mnesia is that if it is set up correctly,
data will automatically be replicated across a cluster.
Normally, when responding to a HTTP request, we return a status of
200 OK. However, here we are creating a
new resource, so returning a status of 201
Created makes sense. The body could be blank or contain any
relevant information such as the name and ID of the airport. In this case
we return the JSON that was sent by the browser, as the ExtJS framework
expects that.
Example 4-12. Generating the content
handle('POST',Arg)->{ok,Json,_}=rfc4627:decode(Arg#arg.clidata),io:format("~n~p:~pPOST request~p~n",[?MODULE,?LINE,Json]),Airport=rfc4627:get_field(Json,"airport",<<>>),City=rfc4627:get_field(Json,"city",<<>>),Country=rfc4627:get_field(Json,"country",<<>>),Name=rfc4627:get_field(Json,"name",<<>>),_Status=addAirport(Airport,City,Country,Name),[{status,201},{html,Arg#arg.clidata}];