1

I'm trying to submit a GET request from a web service and I want to pass a JSON argument as a parameter in the HTTP header. I have the following code that performs the get request without the JSON argument. How would I add the JSON argument in the HTTP body to pass the JSON parameter?

Heres is my code that works without the JSON argument:

-(void) getReposByDate:(void (^)(NSMutableArray *))handler
{
    //Get credentials
    NSDictionary *credentials = [KeychainUserPass load:@"APP NAME"];

    NSString *userName = [credentials allKeys][0];
    NSString *password = credentials[userName];

    //Create request
    NSString *requestString = @"SOME WEB SERVICE URL";
    NSURL *url = [NSURL URLWithString:requestString];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    NSData *userPasswordData = [[NSString stringWithFormat:@"%@:%@", userName, password] dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64EncodedCredential = [userPasswordData base64EncodedStringWithOptions:0];
    NSString *authString = [NSString stringWithFormat:@"Basic %@", base64EncodedCredential];

    NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfig.HTTPAdditionalHeaders=@{@"Authorization":authString};

    self.session=[NSURLSession sessionWithConfiguration:sessionConfig];

    NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSMutableDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        NSLog(@"%@", jsonObject);
        handler([jsonObject valueForKeyPath:@"name"]);

    }];

    [dataTask resume];

}
user2604504
  • 697
  • 2
  • 14
  • 29
  • Does it have to be a header on a `GET` request? That's a very unusual way of doing it. Generally you'd just make a `POST` request with a `Content-Type` of `application/json` and then include the JSON in the body of the request, not the header. – Rob Jun 27 '14 at 07:14

0 Answers0