Easy Uploading "PUT" File Using AFNetworking
I guess it is pretty simple, but I couldn't figure out how to do it. I want to upload a file using a PUT request to a web service using the AFNetworking library. This is the curl command I used to test the service
mac:~ user$ curl --verbose -T image.jpeg http://server.org:8001/social/test.jpg
* About to connect() to server.org port 8001 (#0)
* Trying 123.45.123.123...
* connected
* Connected to server.org (123.45.123.123) port 8001 (#0)
> PUT /social/test.jpg HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: server.org:8001
> Accept: */*
> Content-Length: 78341
> Expect: 100-continue
>
< HTTP/1.1 100 CONTINUE
< Server: cx1193719-b
< Content-Type: Text/Html
< Accept-Ranges: bytes
< Content-Length: 0
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Server: cx1193719-b
< Content-Type: Text/Html
< Accept-Ranges: bytes
< Content-Length: 0
<
* Connection #0 to host server.org left intact
* Closing connection #0
I was able to upload a file using POST and generate data for other web services, I used PUT requests (with AFHTTPClient and putPath), but still I don't understand how to do this simple file upload.
Thank you for your help!
source to share
This is part of the AFNetworking FAQ. Check out https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ .
Also, if you are using a subclass AFHTTPClient
, you may need to extend it with something similar to:
- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
data:(NSData*)data
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
as suggested in this answer: fooobar.com/questions/2222571 / ... .
With that implemented and your base url subclass AFHTTPClient
set to http://server.org
, you can go ahead and call:
NSData *imageData = [NSData dataWithContentsOfFile:pathToYourJPEGFile];
[client putPath:@"social/test.jpg"
parameters:nil
data:imageData
success:yourSuccessBlock
failure:yourFailureBlock];
source to share
I am using AFNetworking 2.0 and was wondering the same thing. This is what I ended up with:
// manager needs to be init'd with a valid baseURL
NSURL *baseURL = [AfarHTTPSessionManager sharedManager].baseURL;
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
NSData *imageData = UIImageJPEGRepresentation(draftHighlight.largeImage, 1);
// need to pass the full URLString instead of just a path like when using 'PUT' or 'POST' convenience methods
NSString *URLString = [NSString stringWithFormat:@"%@%@", baseURL, _the_rest_of_your_path];
NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:kCreateHighlightAPIKeyImage fileName:@"highlight_image.jpg" mimeType:@"image/jpeg"];
}];
// 'PUT' and 'POST' convenience methods auto-run, but HTTPRequestOperationWithRequest just
// sets up the request. you're responsible for firing it.
AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
// success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// failure
}];
// fire the request
[requestOperation start];
source to share
djibouti33's answer is pretty good in my opinion. However, it should be noted that the multipartFormRequest method in the response has since been deprecated. AFNetworking now recommends using:
- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(NSDictionary *)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
error:(NSError *__autoreleasing *)error
Note that you will want to pass the error by reference by doing something like the following:
NSError *requestError = nil;
NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:photoData name:@"file" fileName:@"image" mimeType:@"image/png"];
} error:&requestError];
source to share