I am trying to send a POST request containing an audio file and some other data using the code below. The server is coded in PHP and although the request is sent and the server is contacted it seems that there is no POST data available. The code below simply takes an audio file, and 2 other strings called cubeid
and request_type
and sends them as POST variables.:
// Create the request.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@smartcube.php",[SMCGlobal apiURL]]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"track.m4a"];
NSURL* soundURL = [NSURL URLWithString:plistPath];
NSData *soundData = [NSData dataWithContentsOfURL:soundURL];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"track.m4a\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: audio/m4a\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:soundData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Text parameter1
NSDictionary* dic = [SMCGlobal returnUser];
NSString *param1 = @"dhshahds";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"cubeid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"request_type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"sendSpeech"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError *connectionError){
NSLog(@"hehe");
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(connectionError){
NSLog([connectionError localizedDescription]);
}else{
NSError *error;
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if(error){
NSLog([error localizedDescription]);
}
NSLog(@"haha");
}
}];
When i try to error_log(json_encode($_POST))
on the server it returns an empty string.
A couple of observations:
This code is sending two requests, one XML request (with no XML body; lol) and one multipart request. And you proceed to mutate the original request to be used in the second request. That doesn't seem like it could possibly be correct.
I presume you should eliminate the Content-Type
of application/xml
line, as well as the starting of the conn
connection.
I would advise against a boundary that, itself has a bunch of leading hyphens. It's not a problem, but it just makes it really hard to interpret if you're observing the raw request in a tool like Charles because it's not immediately obvious which hyphens are part of the multipart delimiter syntax and which are part of the boundary.
Personally, I'd use [[NSUUID UUID] UUIDString]
for my boundary.
As Wain suggests, I would inspect this in Charles and see if the request looks well-formed there.