希望这可以帮助!!!
typedef enum RequestTypes{
GET,
POST,
PUT,
DELETE
}RequestType;
@property (nonatomic, strong) AFHTTPSessionManager *manager;
self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:urlString]]; // Base URL here, not entire URL
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
//Add Headers to request if any. // header is a dictionary of key value pair.
for (NSString *key in [headers allKeys]) {
[self.manager.requestSerializer setValue:[headers valueForKey:key] forHTTPHeaderField:key];
}
/* Serialize request by type */ /* Type can be GET, POST etc */
NSString *requestTypeMethod = [self getStringForRequestType:type];
/* enter your URL here excluding base url */
NSMutableURLRequest *request = [self.manager.requestSerializer requestWithMethod:requestTypeMethod URLString:[[NSURL URLWithString:urlString relativeToURL:_manager.baseURL] absoluteString] parameters:params error:nil];
NSURLSessionDataTask *dataTask = [self.manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
// here you will get actual response, NSURL response as well and error if any.
}];
[dataTask resume];
#pragma mark - GET Request type as string
-(NSString *)getStringForRequestType:(RequestType)type {
NSString *requestTypeString;
switch (type) {
case GET:
requestTypeString = @"GET";
break;
case POST:
requestTypeString = @"POST";
break;
case PUT:
requestTypeString = @"PUT";
break;
case DELETE:
requestTypeString = @"DELETE";
break;
default:
requestTypeString = @"GET";
break;
}
return requestTypeString;
}