Download Zip file to AFNetwoking 3.0

I use AFNetworking 3.0 to upload a zip file to the server, but it gives the following error from the server.

Code

AFHTTPSessionManager  *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www,baseurl.com"]];


    [manager.requestSerializer setValue:@"headertoeknvalue" forHTTPHeaderField:@"Authorization"];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];


    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];

    [ manager POST:@"sync.php" parameters:dictPost constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        NSData *data = [[NSFileManager defaultManager] contentsAtPath:[APP_DELEGATE.strDocumentDirectoryPath stringByAppendingString:@"/Database.zip"]];

 [formData appendPartWithFileData:data
                                    name:@"sync_file"
                                fileName:@"Database.zip" mimeType:@"application/json"];

    } progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSLog(@"Response: %@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        NSLog(@"error: %@", error.description);

    }];

Mistake

[Error] POST  (401) [7.6721 s]: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x79735300> { URL:  } { status code: 401, headers {
    "Cache-Control" = "no-cache";
    Connection = "Keep-Alive";
    "Content-Type" = "application/json";
    Date = "Mon, 05 Sep 2016 06:32:54 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/1.0.1e-fips mod_bwlimited/1.4";
    "Transfer-Encoding" = Identity;
    "X-Powered-By" = "PHP/5.5.38";
} }, NSErrorFailingURLKey=, NSLocalizedDescription=Request failed: unauthorized (401), com.alamofire.serialization.response.error.data=<7b227374 61747573 223a6661 6c73652c 22737461 74757343 6f646522 3a343031 2c226d65 73736167 65223a22 546f6b65 6e206162 73656e74 227d>}
0
source share
1 answer

401 Error Code=-1011 "Request failed: unauthorized (401)"Check if your token is valid or not

Your problem is to exchange strings with your forwarding authorization before we serialize the request so that you need to exchange strings as shown below.

AFHTTPSessionManager  *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www,baseurl.com"]];

manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager.requestSerializer setValue:@"headertoeknvalue" forHTTPHeaderField:@"Authorization"];
+1
source

Source: https://habr.com/ru/post/1625439/


All Articles