【问题标题】:How to upload binary data using AFNetworking 2.0如何使用 AFNetworking 2.0 上传二进制数据
【发布时间】:2014-01-06 08:58:01
【问题描述】:

我正在寻找执行此操作的方法

curl -u username:password -H "Content-Type: application/binary" \
   --data-binary @file.dat -X POST \
   "https://helpdesk.zendesk.com/api/v2/uploads.json?filename=myfile.dat&token={optional_token}"

我试过用这个

[afnetworkmanager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [formData appendPartWithFileData:attachment.attachmentData name:@"image" fileName:attachment.fileName mimeType:attachment.mimeType];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
       // TODO:

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       // TODO:
    }];

但这并没有帮助。目前我收到一个错误提示

{
>     "Cache-Control" = "max-age=0, private, must-revalidate";
>     Connection = "keep-alive";
>     "Content-Length" = 345;
>     Server = "nginx/1.4.2";
>     Status = "201 Created";
>     "X-Content-Type-Options" = nosniff;
>     "X-Runtime" = "1.471856";
>     "X-UA-Compatible" = "IE=Edge,chrome=1";
>     "X-Zendesk-API-Version" = v2;
>     "X-Zendesk-API-Warn" = "Removed restricted keys [\"image\"] from parameters according to whitelist"; } },

> NSLocalizedDescription=Request failed: unacceptable content-type:
> text/plain}

我不明白我错在哪里。

【问题讨论】:

  • Nalin,你是如何提供zendesk认证的?我也在尝试这样做,到目前为止还没有成功。你能发布允许这个工作的正确答案吗?谢谢!

标签: ios afnetworking-2 zendesk


【解决方案1】:

我最终通过使用 AFURLSessionManager 来实现这一点。完整代码如下:

NSString *uploadAttachmentURL = @"https://mydomain.zendesk.com/api/v2/uploads.json?filename=screenshot.jpeg";

        NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        _afHTTPSessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];

        // hack to allow 'text/plain' content-type to work
        NSMutableSet *contentTypes = [NSMutableSet setWithSet:_AFOpManager.responseSerializer.acceptableContentTypes];
        [contentTypes addObject:@"text/plain"];
        _afHTTPSessionManager.responseSerializer.acceptableContentTypes = contentTypes;

        [_afHTTPSessionManager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"[USERNAME]" password:@"[PASSWORD]"];



        [_afHTTPSessionManager POST:uploadAttachmentURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileData:imageData name:@"screenshot" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
        } success:^(NSURLSessionDataTask *task, id responseObject) {
            DDLogError(@"screenshot operation success!  %@", responseObject);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            DDLogError(@"Operation Error: %@", error);
        }];

【讨论】:

    【解决方案2】:

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

        configuration.HTTPAdditionalHeaders = @{@"CS090-Version":[NSString stringWithFormat:@"ios_%@",VERSION];
    
        AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:configuration];
    
    
        manager.responseSerializer.acceptableContentTypes  = [NSSet setWithObject:@"text/html"];
    
        NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:apiURL parameters:nil constructingBodyWithBlock:nil error:nil];
    
        NSURLSessionUploadTask *uploadTask;
        uploadTask = [manager uploadTaskWithRequest:request fromData:imgData progress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
            if (error) {
                NSLog(@"Error: %@", error);
            } else {
                NSLog(@"%@ %@", response, responseObject);
            }
        }];
    
        [uploadTask resume];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-30
      • 2013-11-19
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多