The below code is used in an iPad app to send an HTTP request to a Node.js web server, which produces the following error, but works fine w/ a regular HTML+browser form.
The server is Node.js + formidable which has a multipart parser that only dies on this line of code with this error:
message: parser error, 0 of 29162 bytes parsed
stack: Error: parser error, 0 of 29162 bytes parsed at IncomingForm.write (/usr/local/lib/node/.npm/formidable/0.9.8/package/lib/formidable/incoming_form.js:120:17) at IncomingMessage. (/usr/local/lib/node/.npm/formidable/0.9.8/package/lib/formidable/incoming_form.js:73:12)
at IncomingMessage.emit (events:27:15) at HTTPParser.onBody (http:100:23) at Stream.ondata (http:763:22) at IOWatcher.callback (net:494:29) at node.js:768:9
This is the iPad code:
NSMutableURLRequest * theRequest = [[NSMutableURLRequest alloc] initWithURL:url];
[theRequest setTimeoutInterval:60];
[theRequest setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[theRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
//media
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"iosaudio.cai\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:theAudio]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[theRequest setHTTPBody:body];
Is the request being sent malformed? If so, why and how should it be done?