How to set HTTP request body using AFNetwork's AFHTTPRequestOperationManager?

前端 未结 7 1863
死守一世寂寞
死守一世寂寞 2020-12-13 14:49

I am using AFHTTPRequestOperationManager (2.0 AFNetworking library) for a REST POST request. But the manager only have the call to set the parameters.

-((         


        
7条回答
  •  误落风尘
    2020-12-13 15:36

    May be we can use the NSMutableURLRequest, here is the code :

    NSURL *url = [NSURL URLWithString:yourURLString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];
    
    [request setHTTPMethod:@"POST"];
    NSData *JSONData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil];
    NSString *contentJSONString = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
    [request setHTTPBody:[contentJSONString dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
    

提交回复
热议问题