Get key-value pairs from POST body in Restlets
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:
开发者_如何学PythonForm newForm = new Form(getRequest().getEntity());
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());
}
精彩评论