tags:

views:

31

answers:

1

This may or may not be the dumbest question ever.

I'm using Restlet. When the client (which I do not control) POSTs to the URL, I can call the function:

representation.getText();

Which produces the following example list of key-value pairs in string form:

CallStatus=in-progress&CallerCountry=US&CalledZip=24013&ApiVersion=2008-08-01&CallerCity=ARLINGTON&CalledCity=ROANOKE&CallSegmentGuid=&CalledCountry=US&DialStatus=answered&CallerState=VA&CalledState=VA&CallerZip=22039

How can I access this data as a Map of key-value pairs in Restlet?

ANSWER:

Form newForm = new Form(getRequest().getEntity());
+2  A: 

Check out these examples quoted from restlet.org (a Form is like a Map):

Getting values from a web form

The web form is in fact the entity of the POST request sent to the server, thus you have access to it via request.getEntity(). There is a shortcut which allows to have a list of all input fields :

Form form = request.getEntityAsForm();
for (Parameter parameter : form) {
  System.out.print("parameter " + parameter.getName());
  System.out.println("/" + parameter.getValue());
}

Getting values from a query

The query is a part of the identifier (the URI) of the request resource. Thus, you have access to it via request.getResourceRef().getQuery(). There is a shortcut which allows to have a list of all "key=value" pairs :

Form form = request.getResourceRef().getQueryAsForm();
for (Parameter parameter : form) {
  System.out.print("parameter " + parameter.getName());
  System.out.println("/" + parameter.getValue());
} 
Jim Ferrans
Awesome, "getEntityAsForm()" was what I was looking for. The documentation says its depreciated and will be removed in version 2.1, I think now the "accepted" way to do it is: new Form(getRequest().getEntity())
DutrowLLC
Ah good catch! I'll have to look again at our code and make sure we're using the newer form. Good luck!
Jim Ferrans