Jump to content
We've recently updated our Privacy Statement, available here ×

Issue with creating resource by REST WS using multipart content-type


mateusz.moska

Recommended Posts

Hi,

I've got a problem with creating the simple resource by putting resource descriptor into multipart request. I have been looking for the solution on jasper server community, but I couldn't have found an answear. If I try to create a resource, the same as is in the documentation, I will get Error 400 Bad Request: "Invalid Resource Descriptor" and in the jasperserver.log file I've got an exception:

2012-12-10 11:41:55,054 ERROR RESTResource,http-18080-6:237 - Unexpected error during resource descriptor marshaling: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file. at
org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) at
com.jaspersoft.jasperserver.rest.services.RESTResource.doPut(RESTResource.java:220) at
com.jaspersoft.jasperserver.rest.RESTAbstractService.execute(RESTAbstractService.java:113) ...

In the attached files there are two clients create in JAVA and C#, both cause the same error. What is interesting, if I send the seme resource descriptor by PUT method, but without multipart, every thing goes fine. Unfortunately in the future I will need the request with multipart, because I want to create resource with file from remote system (I can not use SOAP based WS).

Best Regards

Link to comment
Share on other sites

  • Replies 17
  • Created
  • Last Reply

Top Posters In This Topic

Hi i had the same issue. The documentation is not true.

To create a new folder, dont use multipart/form-data but just text/plain. see my example below :

in this example body=data

headers =
     Cookie : JSESSIONID=9D19A14DB810A623C9475673A86A5BEE; Path=/jasperserver
     content-type : text/plain
     User-Agent : JasperServer-Python
data =
 your XML resource descriptor
 
 
 
Reponse =
     status : 201
     content-length : 0
     expires : Thu, 01 Jan 1970 01:00:00 CET
     server : Apache-Coyote/1.1
     pragma : No-cache
     cache-control : no-cache
     date : Thu, 13 Dec 2012 09:54:01 GMT
     p3p : CP="ALL"
     content-type : text/xml;charset=UTF-8
Link to comment
Share on other sites

 

Hi Chrispohe_1,
Thanks for the response, but I think you don't understand me. Creating a folder was just a simple example, in the future I want to create more complicated resources like, jrxml files, images ...
The documentation said: "If the resource has one or more file resources, they must be provided using a multipart request." So I think this is only one posibility to put resourceDescriptor and file content together by REST webservice.
 

 

Link to comment
Share on other sites

Sorry for the empty post above.

Hey,

To put or post a resource including resourcedescriptor and file resource, see my example below

headers =
     Cookie : JSESSIONID=96F74EFB25567E3F0CD872652EAF3FBB; Path=/jasperserver
     Content-Length : 1649
     Content-Type : multipart/form-data; boundary=643e0c37cf39452eb005e5be13a4da72
     User-Agent : JasperServer-Python
data =
--643e0c37cf39452eb005e5be13a4da72
Content-Disposition: form-data; name="ResourceDescriptor"
Content-Type: text/plain; charset=utf-8
 
your xml resourcedescriptor with property has data set to true
 
--643e0c37cf39452eb005e5be13a4da72
Content-Disposition: form-data; name="/images/imagetest.png"; filename="/var/lib/tomcat6/webapps/jasperserver/wcf/tree/leaf.png"
Content-Type: image/png
 
--643e0c37cf39452eb005e5be13a4da72--
 
Reponse =
     status : 201
     content-length : 0
     expires : Thu, 01 Jan 1970 01:00:00 CET
     server : Apache-Coyote/1.1
     pragma : No-cache
     cache-control : no-cache
     date : Thu, 13 Dec 2012 12:30:09 GMT
     p3p : CP="ALL"
     content-type : text/xml;charset=UTF-8
 
the name of the second argument must be the same as the uriString
 
Link to comment
Share on other sites

Hi,

The answer was very helpful, thanks a lot!
The example in documentation is incorrect. You can't create resource using multipart content-type without sending a file content.

In my example you can create folder using multipart content type, but you have to add dummy file as one part in the request.

Link to comment
Share on other sites

I know, I told you the documentation is false and incomplete

FYI, i found a good code example lost somewhere in this website : http://community.jaspersoft.com/wiki/getting-started-rest-web-service-api

you'll find java code example at the bottom of page. Just read the code in the following path :

codeexamplesrctestjavacomjaspersoftjasperserverrestsampleBasicResourceCRUDTest.java

I hope i help a lot of people, because i never found any informations about creating resource with RESTapi

Link to comment
Share on other sites

  • 2 months later...

Hi Chris, Appreciate your response.

I have copied a jrxml from the server by GET method and now I am trying to put that back to the same location meaning I need to use addormodify, in REST API as per documentation I have to use POST method with resource service. I created a multi part request as per the above example. I am pasting the pseudo-code below

MultipartEntity multipartEntity = null;

HttpPost generateReport = new HttpPost("http://localhost:8070/jasperserver/rest/resource/pathtoresource");

multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

multipartEntity.addPart("ResourceDescriptor", new StringBody(requestXml));

multipartEntity.addPart("http://localhost:8070/jasperserver/rest/resource/pathtoresource", new FileBody(new File("location of jrxml")));

generateReport.setEntity(multipartEntity);

HttpResponse httpResponse = httpClient.execute(generateReport, localContext);

 

Before this code I am calling the login function an I am getting status 200 but then 403.

Anjali

Link to comment
Share on other sites

You say : "I have copied a jrxml from the server by GET method and now I am trying to put that back to the same location meaning I need to use addormodify, in REST API as per documentation I have to use POST method with resource service"

 

Exact !

 

You say : "http://localhost:8070/jasperserver/rest/resource/pathtoresource"

the documentation is not exact.

You have to use pathtoresource for PUT method BUT pathtoresource/resourcetomodify for POST method.

 

You can't use POST in a folder. You need to know name the of resource you want to modify.

 

Do you understand i mean ?

Link to comment
Share on other sites

Thanks for your reply. Exactly that's what I am trying to do. I am doing a POST and then in the above statement "http://localhost:8070/jasperserver/rest/resource/pathtoresource" here actually path to resource is the path to the reportunit, which is what I need to modify.

Similarly in the same API I have a need to copy a reportunit from one folder to another within the jasperserver. For that I am using PUT which is giving me 400 error.

Link to comment
Share on other sites

One more thing I will like to share, the way I am trying to capture cokkies

 

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpContext localContext = new BasicHttpContext();

 

 

try{

HttpClient client = new HttpClient();

 

// Setting Login URL in a POST method

String loginURL = "http://localhost:8070/jasperserver/rest/login";

HttpPost loginCall = new HttpPost("http://localhost:8070/jasperserver/rest/login");

PostMethod postMethod = new PostMethod(loginURL);

httpClient.getCredentialsProvider().setCredentials(

new AuthScope("http://localhost", 8070),

new UsernamePasswordCredentials("username", "password"));

 

cookieStore = new BasicCookieStore();

// Set authentication parameters

postMethod.addParameter("j_username", "username");

postMethod.addParameter("j_password", "password");

int statusCode = client.executeMethod(postMethod);

if (statusCode != HttpStatus.SC_OK) {

System.out.println("Login failed: " + postMethod.getStatusLine());

return;

}else

System.out.println("The value of login status is:"+postMethod.getStatusLine());

 

 

// Create local HTTP context

// Bind custom cookie store to the local context

localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

 

loginCall.setHeader("content-type", "application/x-www-form-urlencoded");

 

Link to comment
Share on other sites

FYI , it exists several REST clients addons in web browser (Chrome and firefox).

Go to chrome webstore and look for 'REST' term.

My favorite is Postman REST. You'll be able to try the API faster than modify your code.

I spent a lot of time on this API. This tools is really efficient to test the API, and writing the code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...