Hello Soumik,
just curious:
1) What happens if you change your implementation to:
@Post("xml")
public Representation create(final XmlRepresentation xml) {
System.out.println("XML received:\n" + xml.getText());
return null; // Just for completeness of this example implementation
}
Do you see the XML printed?
Beware with streamed representations, they may be consumed by some
method calls, thus you don't have them available anymore after such
consumption happened (probably getAvailableSize() is consuming it
somehow... I don't know)
BTW, with the annotation on the above example, you don't need to check
the media type of the provided representation, that handler will only
be called when XML is POSTed.
2) HTTP already defines a code for 'unsupported media type': it's HTTP
code 415, Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE. Unless you have
very good reasons not to, try to use the standard HTTP codes on your
REST API implementation responses.
Hope this helps, good luck!
On Mon, May 16, 2011 at 2:36 PM, soumik biswas <[email protected]> wrote:
> Hi,
> I've just started using Restlet 2.0.7. I'm trying to build a REST API. But
> I'm facing problem with declaring a method which accepts POST request which
> has content type - application/xml.
> Following is my method which processes POST request:
> ------------------
> @Post
> public Representation create(Representation r)
> {
> Representation rep = null;
> try{
> if(MediaType.APPLICATION_XML.equals(r.getMediaType()))
> {
> System.out.println("XML received of size:" + r.getSize() + ",and
> available size: " + r.getAvailableSize() + ", and content: " + r.getText() +
> ", byteChannel: " + r.getChannel() + ", inputStream: " + r.getStream());
> rep = new
> StringRepresentation(Configuration.parseXml(r.getStream()),MediaType.APPLICATION_XML);
> }
> else
> {
> setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
> rep = new StringRepresentation("Invalid Data in POST. XML expected.",
> MediaType.TEXT_PLAIN);
> }
> return rep;
> }
> -------------------
>
> Now the problem is when I test this API using Firefox REST plugin with the
> following XML data in the body:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <config>
> <system name="something"></system>
> </config>
>
> I get the following output:
>
> XML received of size:125,and available size: 125, and content: null,
> byteChannel: null, inputStream: null.
>
> It seems that though the method is getting the size of the POST data
> correctly, its not able to get the content(String or InputStream) from the
> Representation.
>
> Am I missing something here??
> How do I get the XML content which is sent to this REST API??
>
> Thanks,
> Soumik
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2738272
>
--
Fabián Mandelbaum
IS Engineer
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2738281