2

I have looked at all the similar questions I could find but none seem to have all the right pieces together for my situation, which seems very simple.

  1. I'm creating a simple NSURLRequest using POST and setting its body to an NSDATA build using the NSJSONSerialization class.
  2. I'm testing a very simple php script to capture that data.

  3. The code works fine if I get the value in PHP using json_decode(file_get_contents('php://input'),1); but not at all using $_POST

  4. For security reasons, I'm told the server cannot enable file_get_contents() ... so I'm looking for an alternative.

Here is my Obj-C code to create the request:

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.techno-mac.org/dev101/jsonForm.php"]];
[request setHTTPMethod:@"POST"];

NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
[jsonDict setValue:self.userNameField.text forKey:@"userName"];
NSLog(@"Dict: %@",jsonDict);

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String: %@",jsonString);

//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
NSLog(@"jsonData: %@",jsonData);

self.postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];

Here is the console log when I run it, typing "test" in a field that gets encoded in the json data:

2012-05-17 20:26:19.417 RBWebServices[95037:f803] Dict: {
userName = test;
}

2012-05-17 20:26:19.418 RBWebServices[95037:f803] JSON String: {"userName":"test"}
2012-05-17 20:26:19.418 RBWebServices[95037:f803] jsonData: <7b227573 65724e61 6d65223a   22746573 74227d>
2012-05-17 20:26:19.464 RBWebServices[95037:f803] Received response from server, resetting data length to zero
2012-05-17 20:26:19.465 RBWebServices[95037:f803] Succeeded! Received 12 bytes of data

And here is my very simple php script that works:

$json_post = json_decode(file_get_contents('php://input'),1);

$myArray = $json_post["userName"];
print $myArray;

and the code that doesn't (Returns "Array")

$json_post = json_decode($_POST);
print $json_post["userName"];

If I enter this in php:

var_dump($_POST)

I receive:

array(0){}

Because the first scenario works, I tend to think it is the $_POST that is empty which is the cause... or the way I'm trying to get something out of it, but it seems completely empty.

If this is impossible using $_POST, perhaps there is another way not to use file_get_contents ?

I'm far from being a php expert at all... sorry if this seems trivial.

Thanks for any suggestions you may have.

Just a coder
  • 15,480
  • 16
  • 85
  • 138
rboisjoly
  • 95
  • 1
  • 8

1 Answers1

0

A quick test using the following script and curl demonstrated that $_POST was only populated when using key/value pairs. Otherwise it was considered raw input.

Script:

var_dump($_POST);
var_dump(file_get_contents('php://input'));

Executed (raw data):

curl -X POST -d "whatever" http://jason.local/dev/server.php

Output (empty $_POST):

array(0) {
}

string(8) "whatever"

Executed (key/value pair):

curl -X POST -d "data=whatever" http://jason.local/dev/server.php

Output:

array(1) {
  ["data"]=>
  string(8) "whatever"
}

string(13) "data=whatever"

This may indeed be part of the HTTP Spec or PHP internals. I'm sure someone else can verify that. Nonetheless, the quick and dirty fix would be to use key/value pairs.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • Hi and thanks, this is good to know. I need to send json because the data I'm sending is too complex for key/value pairs though. So although this works, and I use this elsewhere, I was trying to get the json in the body of the request. What you are saying though is that the spec itself may require this and until someone confirms this, I will presume that this is the case. Thanks! – rboisjoly May 18 '12 at 18:08
  • I agree that JSON in the body is cleaner. Especially for complex data. I typically use key/value pairs for my calls since it's often lighter weight. Nonetheless, if you are using a REST API, sooner or later you'll need `php://input` (PUT, DELETE). I am curious if this is indeed part of the spec for POST. I'm attending a PHP conference next week and will ask around. – Jason McCreary May 18 '12 at 18:49