2

I am working on Post method. I have 3 jsons and 3 strings. I want to use them for a post method. I have tried many solutions but it didn't solved. order_item_details, order_details, device_info have json objects

 NSURL *url=[NSURL URLWithString:@"my url"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"]; 
NSMutableData *jsonData = [NSMutableData data];

// add params (all params are strings)
[jsonData appendData:[[NSString stringWithFormat:@"{order_item_details=%@", orderItemDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@",order_details=%@", orderDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@",transaction_id=%@", transID] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@",order_unique_id=%@", unique] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@",user_id=%@", userid] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@",device_info=%@}", deviceInfo] dataUsingEncoding:NSUTF8StringEncoding]];
[request setURL:url];
[request setValue:[NSString stringWithFormat:@"%d", jsonData.length] forHTTPHeaderField:@"Content-Length"];
//            //HTTP Authentication
NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"admin", @"1234"];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];

NSString *authValue = [authData base64Encoding];

[request setValue:[NSString stringWithFormat:@"Basic %@",authValue] forHTTPHeaderField:@"Authorization"];

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if ([response statusCode] >= 200 && [response statusCode] < 300)
{
    NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

    SBJsonParser *jsonParser = [SBJsonParser new];
    NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];

    NSLog(@"%@",jsonData);
}

In response i got null value. I have json like below

 {order_item_details=     [{"item_amount":"500.00","item_price":"100.00","item_size":"M","page_name":"Billi","quantity":"5","fk_image_id":880},{"item_amount":"250.00","item_price":"50.00","item_size":"S","page_name":"Billi","quantity":"5","fk_image_id":880}], 

 order_details=[{"email":"[email protected]","mobile_order_date":"2016-02-11 11:44:16","orderUniqueId":"3204a5eb8be171a51057678233268718302","order_price":"750.00","order_total_amount":"800.00","order_type":"COD","paypal_transaction_id":"","service_charges":"40.00","service_percentage":"5.00","shipping_address":"asad","shipping_cost":"50.0","shipping_name":"asad","shipping_phone":"123456","fk_user_id":141,"no_of_items":2,"order_id":1}], 

 transaction_id=, 
 order_unique_id=3204a5eb8be171a51057678233268718302, 
 user_id=141,
 device_info={"device":"klimtwifi","device_model":"SM-T700","device_brand":"samsung","device_id":"LRX22G","device_serial":"3204a5eb8be171a5","device_manufacturer":"samsung","device_product":"klimtwifixx"}}
7
  • show the format in which you want to send your json data. Commented Feb 15, 2016 at 11:27
  • So this is the required format. Right? Commented Feb 15, 2016 at 11:34
  • no, this is the json, I want to use that for post method Commented Feb 15, 2016 at 11:35
  • What is format required by your server side post request? Commented Feb 15, 2016 at 11:36
  • required json is also same. I have no problem in json. I just received null value Commented Feb 15, 2016 at 11:39

2 Answers 2

1

I have solved the issue, the problem was that the server consider the data as one object. I have changed with the below code

NSMutableData *jsonData = [NSMutableData data];
[jsonData appendData:[[NSString stringWithFormat:@"order_item_details=%@&", orderItemDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"order_details=%@&", orderDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"transaction_id=%@&", transID] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"order_unique_id=%@&", unique] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"user_id=%@&", userid] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"device_info=%@", deviceInfo] dataUsingEncoding:NSUTF8StringEncoding]];
[request setURL:url];
Sign up to request clarification or add additional context in comments.

Comments

0

Create dictionary as below :-

NSDictionary *parameterDictionary = @{@"order_item_details":orderItemDetails, @"order_details":orderDetails, @"transaction_id":transID, @"order_unique_id":unique, @"user_id":userid, @"device_info":deviceInfo};  //Modify the dictionary as per your need

NSData *requestData = [NSJSONSerialization dataWithJSONObject:parameterDictionary options:NSJSONWritingPrettyPrinted error:nil];
[request setHTTPBody: requestData];

Hope this help you out !

1 Comment

I dont want json to post. I want 5 params to post. order items and order details have json, which is perfect. every param has its own value, it should be json or simple string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.