2

I need to Parse below string to POST method in iOS

"jsonData":{"empId":"cxvd","password":"sfsd"}

But I m getting the error as

Res: Tomcat Error

HTTP Status 400 - Required String parameter 'jsonData' is not present

//------ Method I have used to Parse is ---------- //

+(void) requestToServerForLogin:(NSString*)userName andPassward: (NSString*)password  onCompletion:(RequestCompletionHandler) handler
{
    NSString *url = [Ip stringByAppendingString:@"login"];

    NSString *jsonString = [NSString stringWithFormat:@"\"jsonData\":{\"empId\":\"%@\",\"password\":\"%@\"}",
                                                      userName,
                                                      password ];

     NSURL *nsurl = [NSURL URLWithString:url];

     NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:nsurl];
     [urlRequest setTimeoutInterval:60.0f];
     [urlRequest setHTTPMethod:@"POST"];
     [urlRequest setValue:@"application/json"
     forHTTPHeaderField:@"Content-type"];

     NSString *body = jsonString1;

     [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
     NSLog(@"urlRequest :%@",[body dataUsingEncoding:NSUTF8StringEncoding]);

     NSOperationQueue *queue = [[NSOperationQueue alloc] init];

     [NSURLConnection sendAsynchronousRequest:urlRequest
                                       queue:queue
                           completionHandler:^(NSURLResponse *response,
                                               NSData *data1, NSError *error) 
                                             {
                                                  NSString *res = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
                                                  if(handler) handler(res,error);
                                             }];

}

Thanks in advance

1
  • Can any one help me to parse this string "jsonData":{"empId":"1111","password":"####"} after parsing every thing I m getting 'jsonData' is not present Commented Mar 11, 2015 at 6:55

1 Answer 1

2

There is way to much code. The substringToIndex and substringFromIndex are wrong, should not be in the code.

Use the literal syntax for the dictionaries:

NSDictionary *jsonDict = @{@"jsonData":@{@"password":password, @"empId":userName}};
NSData* jsonData =  [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];
Sign up to request clarification or add additional context in comments.

3 Comments

But substringFromIndex I have used to remove flower braces from Json string ,as I m getting below error ---HTTP Status 400 - Required String parameter 'jsonData' is not present
Yes I have used this format earlier which gives me input as -- Input == {"jsonData":{"empId":"wfr","password":"wtrt"}} but i need the input string as==== "jsonData":{"empId":"wfr","password":"wtrt"}
If any alternative solution can you Plz let me know..I have used even Multipart but of no use

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.