AFNetworking Post 요청

<span style="color:#ff0000;">   .h </span>
#import <Foundation/Foundation.h>
#import <AFNetworking.h>
@interface MSHttpRequest : NSObject

+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;

@end

.m 파일
#import "MSHttpRequest.h"

@implementation MSHttpRequest


+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    static NSString *http = @"http://192.168.0.216:8080/app";
    NSString *str = [http stringByAppendingString:url];
   
    // 1. 
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    // 2. 
    manger.requestSerializer = [AFJSONRequestSerializer serializer];
    manger.responseSerializer = [AFJSONResponseSerializer serializer];
    [manger.requestSerializer setValue:@"1.0.0" forHTTPHeaderField:@"app.version"];
    [manger.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"os"];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *loginUserId =  [userDefaults objectForKey:USERLOGINID];
    // NSLog(@"*************************%@",loginUserId);
    [manger.requestSerializer setValue:loginUserId forHTTPHeaderField:@"user.login.id"];

     //[manger.requestSerializer setValue:@"65069F4357BA7A84CC7D865B537FD2DA6A7EA7E24ED05D39D4C6BA1FEAEFEEAB38AACD86384CC1FF" forHTTPHeaderField:@"loginUserId"];
    
//        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//        NSString *loginUserId =  [userDefaults objectForKey:UserHeader];
      // NSLog(@"*************************%@",loginUserId);
    
    
    // 3. 
    [manger POST:str parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

@end

컨트롤러에 요청 데이터 쓰는 방법 쓰고 호출
-(void)request{
NSMutableDictionary *params = [NSMutableDictionary dictionary];

    long para = [self.freeID integerValue];
    DLog(@"%ld",para);
    params[@"spm"] = @(para);
    
       //  
    [MSHttpRequest post:@"/api/freeOrder/userOrderDetail.json"params:params success:^(id json) {
        DLog(@"%@",json);
    } failure:^(NSError *error) {
        DLog(@" %@",error);
    }];
}

좋은 웹페이지 즐겨찾기