How do I configure data for a PUT request using AFNetworking?
I started using AFNetworking and it works well when it receives a simple "GET" -request. However, I am now trying to make a "POST" request. I am using the following code to make a "GET" request. When you look at puthPath from AFHTTPClient , it is not possible to set data for body. I assume there is another way of doing this. I was looking at AFHTTPOperation as a way to fix it. However, I am not getting this to work. The problem is I don't know how to use it with basic authentication.
Can anyone give me a hint how to make a simple "POST" request with AFNetworking?
AFHTTPClient* client = [AFHTTPClient clientWithBaseURL:ServerURL];
[client setAuthorizationHeaderWithUsername:self.username
password:self.password];
NSString* resourcePath = [NSString stringWithFormat:@"/some/resource/%@",
endPath];
[client getPath:resourcePath
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Success code omitted
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Some error handling code omitted
}
];
source to share
I haven't found an easy way to do this. But I did it as recommended and created my own subclass of the AFHTTPClient class . In the subclass, I have implemented the below methods. This allows for both POST and PUT requests with my own data.
- (void)postPath:(NSString *)path
parameters:(NSDictionary *)parameters
data:(NSData*)data
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
{
NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters data:data];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}
- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
data:(NSData*)data
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
{
NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters data:data];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}
-(NSMutableURLRequest*)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
data:(NSData*)data;
{
NSMutableURLRequest* request = [super requestWithMethod:method
path:path
parameters:parameters];
[request setHTTPBody:data];
return request;
}
source to share
With AFNetworking 2.0 I just copy the code from
- (AFHTTPRequestOperation *)PUT:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
and add
[request setHTTPBody:data];
Here he is:
NSString* str = [bookDetailLink objectForKey:@"Body"];
NSData* data = [str dataUsingEncoding: NSUTF8StringEncoding];
NSMutableURLRequest *request = [self.manager.requestSerializer requestWithMethod:@"PUT" URLString:bookingDetailUrl parameters:nil error:nil];
[request setHTTPBody:data];
AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *op, NSHTTPURLResponse *response) {
NSLog(@"%@", response);
}
failure:^(AFHTTPRequestOperation *op, NSError *error) {
NSLog(@"%@", error);
}];
[self.manager.operationQueue addOperation:operation];
I am integrating the Skyscanner API into our iOS app using AFNetworking.
source to share
With AFNetworking 1.3.2, the following code works for me:
NSData *imageData = UIImageJPEGRepresentation(thumb, 0.85F);
AFHTTPClient *httpClient = [[AFHTTPClient alloc]
initWithBaseURL:[NSURL URLWithString:@"https://example.com/"]];
NSMutableURLRequest *request = [httpClient
requestWithMethod:@"PUT" path:@"/foo" parameters:nil];
[request setHTTPBody:imageData];
[request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [httpClient
HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *op, NSHTTPURLResponse *response) {
NSLog(@"%@", response);
}
failure:^(AFHTTPRequestOperation *op, NSError *error) {
NSLog(@"%@", error);
}];
[operation start];
This results in a PUT request with correct headers, Content-Lenght, and overall RESTfulness :-)
source to share