iOS (objective c) send post request with json to php backend
NickName:ManAtMiddleWaterloo Ask DateTime:2016-08-10T19:09:43

iOS (objective c) send post request with json to php backend

My code in iOS:

NSString * some_str = @"{\"one\":\"two\",\"three\":\"four\"}";
NSData *objectData = [some_str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                     options:NSJSONReadingMutableContainers
                                                       error:&error];  
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager POST:@"https://exmaple.com/post.php" parameters:json progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"Error: %@", error);
}];

My code in backend (PHP):

<?php
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
    throw new Exception('Request method must be POST!');
}
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
    throw new Exception('Content type must be: application/json');
}

//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
echo $content;


// $responses = array();
header("Content-type: application/json, charset=utf-8");
// echo json_encode($responses);

$conn->close(); 

?>

If I echo $content, the response received by iOS will be some hex number. i.e. 3c21444f 43545950 (hex number for the json string start from the tail to the head connected by a "&" sign, i.e. three=four&one=two). If I echo $decoded, it returns an empty json.

And if I try to decode the json received by Post request, it failed. I believed that the received object was a possible input for json_decode function.

Should I rewrite my iOS code or PHP code or both?

Copyright Notice:Content Author:「ManAtMiddleWaterloo」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38871781/ios-objective-c-send-post-request-with-json-to-php-backend

More about “iOS (objective c) send post request with json to php backend” related questions

iOS (objective c) send post request with json to php backend

My code in iOS: NSString * some_str = @"{\"one\":\"two\",\"three\":\"four\"}"; NSData *objectData = [some_str dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *json = [NSJSONSerializa

Show Detail

Objective c post request not sending the data

Good day.Im trying to send simple post data to server.This is the code how i do it. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configurati...

Show Detail

Xcode with objective c to post json data to php api

Am trying to post some user data from ios to my php api, but the data am receiving is not properly formatted as a json file. Please can someone help me out as i am very new to ios and objective c. ...

Show Detail

How to send image attachment to server in post data iOS objective c

Send data to server works in postman but getting error in Objective-C. I tried to achieve this one failed from server. I referred below links does not work. Getting error upload failed. What am I ...

Show Detail

Converting PHP post request format in objective C

I am trying to call webservice created in PHP.The request and response is succesfull when i send this from php : $.post( 'http://166.178.11.18/server/xs/reliable.php', {"name":

Show Detail

Objective-c Send Post request to json-RPC webservice

In iOS i want send a POST request to a json-RPC web service. How can i do this? i read This Page before and this repo but none of them have helped me.

Show Detail

How to post request to php script with request and post variables in objective C

I want to send a post request from objective-C code to a php script with URL http://ggg.abc.com/example.php?MO&amp;[email protected]&amp;message=TESTMSG&amp;recipient=11111 I tried to use

Show Detail

Send array to PHP from objective C using POST/GET

I am trying to send an array of strings from Objective C to PHP. I was using GET initially but I got to know that GET is not suitable for sending large amount of data. My array would have approxima...

Show Detail

CouchDB / iOS - Send HTTP POST request with a image

I have google'd and searched around for a solution but I can't find much. I need to send a HTTP POST request from objective-c to my couchdb. What I need to know is how to add an image to the requ...

Show Detail

Forming a JSON Data request in Objective C

I have to send the following JSON data in request for a web service. So how can I create the following type of JSON using iOS/objective C: { "version": "1.0", "myData": [ { "name&qu

Show Detail