users@jersey.java.net

Problem getting mulitpart data

From: Don Szromba <dszromba_at_socal.rr.com>
Date: Sat, 30 May 2009 13:29:13 -0700

Hello,

I'm writing an app that, in part, needs to send some multipart data
from an iPhone app to a web service (including an image or picture). I
have implemented Jersey MultiPart in a restful service using Netbeans
following patterns I am finding on the web. Deploying to Glassfish 2.1
(and/or Tomcat 6) results in the following error:

The server refused this request because the request entity is in a
format not supported by the requested resource for the requested method

I'm not sure if this is a problem in the Objective-C client or in the
web service.

Any help with this will be greatly appreciated.

This is what I'm doing in the client (for testing I'm just trying to
send an image):

NSData *imageData = UIImagePNGRepresentation(selectedImage);

NSURL *url = [NSURL URLWithString:@"http://192.168.0.100:1417/MyService/resources/cards
"];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest
requestWithURL:url];
        NSString* boundary = @"AaB03x";

        [theRequest addValue: @"multipart/mixed; boundary=AaB03x"
forHTTPHeaderField:@"Content-Type"];

        [theRequest setHTTPMethod:@"POST"];
        
        NSMutableData *body = [NSMutableData data];
        
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r
\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Disposition:
form-data; name=\"cardImage\"\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type:
application/octet-stream\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:imageData];
        
        [body appendData:[[NSString stringWithFormat:@"\r\n--%_at_--\r
\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [theRequest setHTTPBody:body];
        
        NSURLConnection *theConnection = [[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
        

In my service I have this...

     @POST
     @Consumes(MultiPartMediaTypes.MULTIPART_MIXED)

     public Response getFormData(MultiPart multiPart) {
         BodyPartEntity bpe = (BodyPartEntity)
multiPart.getBodyParts().get(0).getEntity();

        String status = "";

        InputStream stream = bpe.getInputStream();
        if (stream != null)
         {
                 status = "got an inputStream!!";
         }

         try
         {
             CardServices services = new CardServices();
             if (status != null && status.length() > 0)
             {
                 if (services.connectionTest(status))
                 {
                     return
Response.status(Response.Status.ACCEPTED).entity("hit service with
action!!!").type(MediaType.TEXT_PLAIN).build();
                 }
                 else
                 {
                     return
Response.status(Response.Status.BAD_REQUEST).entity("got status but
didn't hit service").type(MediaType.TEXT_PLAIN).build();
                 }
             }
             else
             {
                 if (services.connectionTest("nothing"))
                 {
                     return
Response.status(Response.Status.ACCEPTED).entity("hit the service with
nothing").type(MediaType.TEXT_PLAIN).build();
                 }
                 else
                 {
                     return
Response.status(Response.Status.BAD_REQUEST).entity("no status and
didn't hit the service").type(MediaType.TEXT_PLAIN).build();
                 }
             }
         }
         catch(Exception ex)
         {
             return
Response.status(Response.Status.BAD_REQUEST).entity("exception calling
service").type(MediaType.TEXT_PLAIN).build();
         }
     }