1

I am trying to construct a POST request with certain parameters in the body of the message but am struggling with handling / seeing the response.

This is the request I am trying to recreate in Obj C:

enter image description here

I am somewhat familiar with the code to create this request but am struggling with parsing the response into meaningful data. This is what my method currently looks like for this (it appears to be Successful):

  NSLog(@"WEB SERVICE CALLED!");

    NSURL *aUrl = [NSURL URLWithString:@"xxx"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

    [request setHTTPMethod:@"POST"];
    NSString *postString = @"grant_type=password&username=xxx&password=xxx";
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
                                                                 delegate:self];

    if(connection) {
        NSLog(@"Connection Successful");
    } else {
        NSLog(@"Connection could not be made");
    }
}

How can I see what my response is? Thanks in advance for any help!

UPDATE

I added this bit and can see that my response gives me a code 200 (success) but the data object looks like it needs to be serialized as it looks like a hex string. This is what I am doing to handle the response / data / error:

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                           // your data or an error will be ready here
                           NSLog(@"RESPONSE: %@",response);

                           NSLog(@"DATA: %@",data);

                           NSLog(@"ERROR: %@",error);
                       }];

This is what the log for the data object looks like: enter image description here

arcade16
  • 1,535
  • 4
  • 23
  • 45

1 Answers1

5
NSError *error;
     id obj= [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
     if (!obj) {
         NSLog(@"JSON parse error: %@", error);
     } else {
         NSLog(@"obj= %@", obj);
     }