سؤال

I have the following API call:

URL: /api/some-call
Method: PUT
PARAMS: No params

Its just a simple PUT method. I am trying to use AFNetworking to do that and unfortunately, I am failing. Here's what I have right now:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *req = [httpClient requestWithMethod:@"PUT" path:@"" parameters:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:req];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success");
} failure: ^(AFHTTPRequestOperation *operatn, NSError *error) {
    NSLog(@"Failure");
}];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];

This is however, not working. Why is that? Furthermore, what is path supposed to be in a PUT request? I've tried several things and this is what I have now at the end, which I believe should be close to what is correct.

One last question: AFNetworking does not use ARC. Does that mean I still need the autorelease at the end of the NSOperationQueue statement?

EDIT: Here is error NSLog: Failure Error Domain=com.alamofire.networking.error Code=-1011 "Expected status code in (200-299), got 409" UserInfo=0x7a91fb0 {NSErrorFailingURLKey=*the url*/api/some-call, NSLocalizedDescription=Expected status code in (200-299), got 409}

هل كانت مفيدة؟

المحلول

Well. You are getting a 409 error code.

Quoted from http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html :

10.4.10 409 Conflict

The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough

information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.

Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.

Which means the error is caused by your server not with your code. unless you have provided some wrong parameters.

Well. As for the question regarding the "what is the path supposed to be in PUT". Normally I'll put baseURL as the domain name of the server. Which is something like

http://localhost 

then i'll put the path to be something like

@"the/rest/of/the/api/url"

then it's easier to switch between development and production servers with just a switch of a baseURL. :)

And for your last question, "AFNetworking does not use ARC. Does that mean I still need the autorelease at the end of the NSOperationQueue statement?"

Does that mean your project is using ARC with AFnetworking, or AFNetworking WITHOUT ARC.

if it's ARC with AFNetworking, you don't have to. Take a look at this https://github.com/AFNetworking/AFNetworking#arc-support

if it's non-ARC with AFNetworking, you basically have to do all the memory management yourself. :) Hit me up again if you need more info and i'll edit accordingly. :)

Hope i've helped in someway.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top