AFNetworking sending NSMutableDictionary as POST

烂漫一生 提交于 2019-12-12 04:34:28

问题


Im new to using AFNetworking. I managed to make GET request work with some help from a nice guy here in stack overflow.

Now im stuck in making a POST request. I try this code but i always end up in failed block. How come?

-(void) postEventInfo: (NSMutableDictionary *) eventInfoObject{

NSURL *url = [NSURL URLWithString:string];

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:string parameters:eventInfoObject success:^(NSURLSessionDataTask *task, id responseObject) {

    NSLog(@"JSON: %@", responseObject);
    //here is place for code executed in success case

} failure:^(NSURLSessionDataTask *task, NSError *error) {


    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
                                                        message:@"Sorry, try again."
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];

    NSLog(@"Error: %@", [error localizedDescription]);
}];
 }

i get the following error message:

Error: Request failed: internal server error (500)

Thnx..

EDIT1

OK so after testing a little bit more i noticed that the connection actually goes through and inte does what i want to do but it still shows me the error message meaning still going to the fail block.

I THINK my server is sending back text/html could it be something to do with that?

the part were NSURLConnection worked

EDIT2

    NSError *error;
NSData *event = [NSJSONSerialization dataWithJSONObject:eventInfoObject
                                                options:NSJSONWritingPrettyPrinted
                                                  error:&error];

if (! event) {
    NSLog(@"Got an error: %@", error);
} else {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:event];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[event length]] forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setURL:[NSURL URLWithString:string]];

    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    [conn start];
}

回答1:


If your server is not setting Content-Type header of application/json (or something equivalent), AFHTTPSessionManager will fail. You could theoretically jury-rig the AFNetworking acceptableContentTypes value, but better than that, you should just fix the server code to return the appropriate Content-Type header.

But if your server is responding with a 500 code, then you have some more fundamental problem, one that we can't possibly solve without more information about the server (e.g. code, some details about the implementation, etc.).

If you have a working NSURLConnection example, I'd be inclined to run both that code, as well as the above code, and observe the requests via Charles. Look carefully at differences in the requests, and the problem will probably jump out at you.




回答2:


So the problem is that with AFNetworking you set base URL for your HTTP manager e.g

http://api.example.com/

But then when you post requests you use endpoint (path relative to base URL) instead of absolute URL, e.g

[manager POST:@"users/add" ...

Which AFNetworking automatically transforms into absolute URL behind the stage:

http://api.example.com/users/add

This is clearly done for convenience of building API clients, that's why POST, GET and other methods take string argument instead of NSURL.

In your particular case changing

[manager POST:string ...

to

[manager POST:@"/" ...

Should efficiently fix the problem.



来源:https://stackoverflow.com/questions/27280648/afnetworking-sending-nsmutabledictionary-as-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!