Question

I'm using NSMutableURLRequest to send data to my PHP server. This is how I create the request:

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[@"whatever" objectForKey:@"message"]] dataUsingEncoding:NSUTF8StringEncoding]];  //converting strings to NSData
[body appendData:[textParams objectForKey:key]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

My question is, in what format is the data received by the server in? I.E. $_POST['message'], would that be in binary, hex, base64, regular string?

Was it helpful?

Solution

The "message" value will come through as a string. In your PHP you can do:

$message = $_POST['message'];

Though your code has an issue. You need the following lines per text value you want to post:

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"message"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *text = ... // the text value to post for "message"
[body appendData:[text dataUsingEncoding:NSUTF8StringEncoding];  //converting strings to NSData
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

You were trying to call objectForKey: on a string literal. You can't do that. Also, you were adding textParams. I don't know what that is supposed to be but you need the above lines for each value you post. Perhaps those params need to be part of text.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top