How do I set the data for a “PUT” request with AFNetworking?

后端 未结 3 1715
悲&欢浪女
悲&欢浪女 2020-12-16 08:33

I have started to use AFNetworking and it works well when it gets to making simple \"GET\"-request. However now I am trying to do a \"POST\"-request. I use the code below to

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 08:49

    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 general RESTfulness :-)

提交回复
热议问题