Search code examples
iosjsonspringmultipart

iOS when send json file to server, it shows required MultipartFile parameter 'file' is not present


I was trying to send json file to java api link.But it shows Required MultipartFile parameter 'file' is not present and error 400(3840). And when I try to use terminal to try to upload it(curl http://inav.zii.io/inav/yusunsite/room12201/data -F "file=@/Users/Apple/Desktop/111-tt.json"), it shows ok.

The code is as followings:

    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* filename=[NSString stringWithFormat:@"%@-%@.json", floorPlanId,roomName];
    NSString* foofile = [documentsPath stringByAppendingPathComponent:filename];
    [jsonData writeToFile:foofile atomically:YES];
    NSData *fileData=  [NSData dataWithContentsOfFile:foofile];
    NSMutableURLRequest *requestPost=[NSMutableURLRequest requestWithURL:dataUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    //NSLog(@"%@",dataUrl);

    NSString *boundary = @"YOUR_BOUNDARY_STRING";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [requestPost addValue:contentType forHTTPHeaderField:@"Content-Type"];
    [requestPost setHTTPMethod:@"POST"];
    NSMutableData *somebody = [NSMutableData data];
    [somebody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [somebody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; file=\"%@\"\r\n\r\n",filename ] dataUsingEncoding:NSUTF8StringEncoding]];
    [somebody appendData:[@"file" dataUsingEncoding:NSUTF8StringEncoding ]];


    [somebody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [somebody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\" filename=\"%@\"\r\n\r\n",foofile ] dataUsingEncoding:NSUTF8StringEncoding]];
    [somebody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [somebody appendData:[NSData dataWithData:fileData]];
    [somebody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    // close form
    [somebody appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [requestPost setHTTPBody:somebody];

The debug console shows:

2014-06-23 10:06:20.756 iBeacons[6258:60b] error parsing JSON response: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x17d3ecb0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
2014-06-23 10:06:20.758 iBeacons[6258:60b] returnString: <html><head><title>Apache Tomcat/7.0.52 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - Required MultipartFile parameter 'file' is not present</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Required MultipartFile parameter 'file' is not present</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.52</h3></body></html>

Solution

  • I have solved problem by using the following codes:

    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:dataUrl]; 
    [request setDelegate:self]; [request setFile:foofile forKey:@"file"]; 
    [request startAsynchronous];
    

    Using ASIHttpRequest could save a lot time, I think.