Afnetworking 2.0 PUT image - objective-c

I am able to use put method of afnetworking 2.0 successfully for putting data.
NSString* PUTURL = [NSString
stringWithFormat:#"%#/updateestado/estado/%#/idJugador/%ld",BASEURl,[status
urlEncodeUsingEncoding:NSUTF8StringEncoding],userId];
NSLog(#"REG URL----%#",PUTURL);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager PUT:PUTURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
[self.NET_Delegate DelegateUpdateStatusResponce:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self.NET_Delegate DelegateUpdateStatusError:error];
}];
I am able to upload video successfully using post method.
But The requirement for uploading image is using PUT
I followed the way given in
Simple "PUT" file upload with AFNetworking
but had two issues says multipartFormRequestWithMethod deprived &
when trying the approach get 404 error.
No reference for this on afnetworking doc in github.
Query: I am working on uploading image using put first time, so i think i am missing some thing. Any reference or code samples to achieve this will be helpful. Thanks

You are not uploading an image; but rather a URL of the image (note the url parameter).
Therefore you will need to upload the image to a 3rd party site and then post the link to whatever that service is.
It's impossible to upload an image using a PUT request so you must be missing something.

Related

upload file use uploadTaskWithRequest:fromFile: in background and use multipart/form-data

here is the situation:
my server only accept multipart/form-data upload action.
my app need to implement background upload.
I've tried below:
if I ignore background, I can use uploadTaskWithRequest:fromData:, build all boundary, content-disposition, and file data, then upload to a supported server. and I do successfully have done this. BUT I need to use background transfer.
if I force this method in background mode, I got an error: Upload tasks from NSData are not supported in background sessions.
if I use background mode, and use uploadTaskWithRequest:fromFile:, I got something like 'stream ended unexpectedly' from the server, as this question mentions, the best answer suggest people to use fromData which apparently is not my need.
so is there any way to accomplish this? since the server can't change it's support, I NEED background transportation and multipart/form-data content-type both.
Finally, I figure out it's a NSURLSession bug, from this issue, I find a way to upload file with fromFile method successfully (and many SO answers used that already, but it's still not shown in AFNetworking's document.
you just need to write your file to a temp file, and use AF's convenient method the build the multipart part. the backend reason you can find out yourself, here's my code
NSMutableURLRequest *multipartRequest = [[AFHTTPRequestSerializer serializer]
multipartFormRequestWithMethod:#"POST"
URLString:[url absoluteString]
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileURL:[NSURL URLWithString:filename]
name:#"file"
fileName:short_name
mimeType:#"application/octet-stream"
error:nil];
} error:nil];
[[AFHTTPRequestSerializer serializer] requestWithMultipartFormRequest:multipartRequest writingStreamContentsToFile:[NSURL URLWithString:temp_file_name] completionHandler:^(NSError * _Nullable error) {
NSURLSessionUploadTask *task = [bgsession uploadTaskWithRequest:multipartRequest
fromFile:[NSURL URLWithString:temp_file_name]
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[[NSFileManager defaultManager] removeItemAtPath:temp_file_name error:nil];
NSLog(#"=========response=========\n%#", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
[task resume];
}];

How to get JSON parsed object from RKObjectManager response block

I'm sending a getObjectsAtPath request like below. Everything works fine but i'm trying to find a way the have the parsed json response available in the success block. As far as i know only response as string or data is available from the operation.HTTPRequestOperation or an array of mapped objects from the mappingResult.array.
Since i need post treatment based on values specific attributes in the JSON response i'm loins to get the parsed JSON response.
Any idea ? If this is not possible as is, how to parse the responseString using rest kit of AFNetwork parsing mechanism ?
Thanks
[self.objectManager getObjectsAtPath:#"api"
parameters:#{#"a11call": #"wallactivityById"}
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];

AFNetworking Half the time downloading half the data

I'm trying to download a small file, about 5.5kb.
Half of the time it only downloads half of the json, and then of course fails parsing that data. The other half of the time everything works completely normal.
I have a good internet connection.
Im using AFNetworking 2.0. I got it off of the main branch today (1/16/2014).
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[NSString stringWithFormat:#"%#Physicians/Types/?os=1",BaseUrl] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
NSLog(#"Error: %#", operation.responseString);
}];
Here is the exact error, but i already know its a parsing error:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No string key for value in object around character 4480.) UserInfo=0x15ecd010 {NSDebugDescription=No string key for value in object around character 4480.}
This is the sample from their website. And like I said it works half of the time.
Do I need to set some sort of chunk size? Is it because their Github page says the current build is failing?
Interestingly enough, it seems to always fail at the exact same point in the file. I can tell this because in the error, I output the response string.
I am not able to reproduce this in any browser, or in my old app which used asihttprequest
***Update 1/28/2014
I downloaded the current version today, which is passing build, and it still fails. When I save out to disk what it does receive it's 5,391 bytes. Again, it fails at the same point each time. In the middle of the work dentist. There aren't any weird characters there.

Got response data from AFNetworking - now what? (Login/Signup on website via app)

I want to make a login/signup feature for my iphone program, and I decided to use AFNetworking for it. First I send POST request to the website I want to login at, and then get response data from it.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = #{#"username": #"myusername", #"password": #"mypassword"};
[manager POST:#"http://website.com/login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
Data *data = (NSData*)responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
What should I do next in order to authorize myself on the website and get access to the information of the logged in user. The responseObject in my code contains just the html for the login webpage.
Any information would be strongly appreciated!
Thank you in advance!
With this approach i strictly suggest you to use another server module for login. First of all, you could create a sort of Api call method reachable from a client ( eg: /api/login.php ), so passing alle necessaries params. Use https, and/or encrypt username and password with a standard method such as AES-256 or you own custom method (you can also see the OAuth protocol for secure authentication between client and server).
Another way is to use a UIWebView ( for iOS ) or WebView ( for Mac OS ), so calling your webpage you are logged in, it depends from which what you want to do.

Retrieving website via API call from site and display to user

I am developing an iphone app.
The first screen of my app shows a list of services available for the user.
The user than selects one of the services and goes to the next screen where they can order that service.
I need to retrieve the list of services using an api call from my website.
What is the the way to do so in IOS without affecting the user experience? I don't want the user to be waiting for me to retrieve the list of services from my site every time they access that page.
I am using the AFNetworking API to get the list of services now.
Thanks!
AFNetworking sounds good.
Make sure you use it asynchronous and with a completion block
e.g.
NSMutableURLRequest *urlRequest = ...
AFHTTPRequestOperation *request = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
[request setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
... handle your services
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
... handle error
}];
[request start];
if the request is JSON, use the AFJSONRequest class