Jack120
Jack120

Reputation: 223

AFNetworking application/json

Using iOS, I'm trying to communicate with a webservice that requests 3 headers followed by JSON POST data.

I've taken a look at the following AFNetworking snippet which converts a Dictionary to a JSON file. In this case I'm trying to POST both the headers and a JSON file. Let me know if you have any suggestions:

NSURL *url = [NSURL URLWithString:WalletKit_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.parameterEncoding = AFJSONParameterEncoding;
NSDictionary *params = @{@"brand-id" : Brand_Id, @"api-key" : API_Key, @"Content-Type" : @"application/json"};
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"" parameters:params];
AFHTTPRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSURLResponse *response, id JSON) {
    NSLog(@"success");
} failure:^(NSURLRequest *request, NSURLResponse *response, NSError *error, id JSON) {
    NSLog(@"error");
}];

Upvotes: 2

Views: 2016

Answers (1)

tom
tom

Reputation: 19173

You should add the HTTP header fields to the NSMutableURLRequest.

[request addValue:@"foobar" forHTTPHeaderField:@"X-Foo-Bar"];

Upvotes: 4

Related Questions