AFNetworking - How can I PUT and POST raw data without using a key value pair?

后端 未结 6 1004
执念已碎
执念已碎 2020-12-15 22:20

I am trying to make an HTTP PUT request using AFNetworking to create an attachment in a CouchDB server. The server expects a base64 encoded string in the HTTP b

6条回答
  •  粉色の甜心
    2020-12-15 23:07

    Here you have an example sending a raw json:

    NSDictionary *dict = ...
    NSError *error;
    NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:dict
                                                               options:NSJSONWritingPrettyPrinted
                                                                 error:&error];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:YOUR_URL parameters:nil error:nil];
    
    req.timeoutInterval = 30;
    [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [req setValue:IF_NEEDED forHTTPHeaderField:@"Authorization"];
    
    [req setHTTPBody:dataFromDict];
    
    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (!error) {
            NSLog(@"%@", responseObject);
        } else {
            NSLog(@"Error: %@, %@, %@", error, response, responseObject);
        }
    }] resume];
    

提交回复
热议问题