AFNetwoking POST call returns 500 internal server error but server got request parameter
Asked Answered
C

7

9

I'm trying to upload my payment success message to my server. Below are my code

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",myTokenString] forHTTPHeaderField: @"Authorization"];


AFHTTPRequestOperation *operation = [manager POST:@"MYAPI" parameters:paramsDict success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"%@",responseObject);


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"%@",error.localizedDescription);

}];
[operation start];

But I'm getting error code 500 (internal server error). But my server has all the information and API call is success. Can anyone please help me understand why it's entering the error block?

Crossbill answered 28/4, 2016 at 13:15 Comment(11)
Have you tried using Fiddler (telerik.com/fiddler) or Postman (chrome.google.com/webstore/detail/postman/…) to see if its an error on your server?Foundry
@JDx Yes. I have tried in Postman even postman returns the same error. But same call works in Android.Crossbill
@AjithKumar have you tried with AFN3.0?Overunder
Yes @Overunder I have tried in AFN 3.0 tooCrossbill
What is the error definition on server?Bernal
500 is a server error. I am pretty sure this won't be a client problem. If request is processed correctly on the server, most likely the crash happens during response generation. It could be caused by some missing header, e.g. Accept. You should check differences between the headers on the server.Vigilantism
I have to agree with @Sulthan, this is likely an error in your server code, and has nothing to do with AFNetworking. Since it doesn't happen on Android, but on every other client, compare the raw HTTP requests if they're any different.Dewar
There is not enough information in your question to debug your issue. Please create a Minimal, Complete, and Verifiable example of the complete issue (including your server code).Supplejack
@AjithKumar Can you put your post parameters which you are using in android and iOS application. Probably you are facing request parameters posting issue.Magma
@AjithKumar does the android version send any custom header keys and values ?Reimport
@ArjitKumar : 500 is server error. Looks like your server is not getting some thing expected like accept-type, paramsDict (request body) content encoding type. You can first verify accept-type, request encoding type from Postmaster.Culp
T
1

In new AFNetworking version, you don't need a initialize for AFHTTPRequestOperation class to handle request so you just adjust your code as following:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",myTokenString] forHTTPHeaderField: @"Authorization"];

[manager POST:@"MYAPI" parameters:paramsDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error.localizedDescription); 
}];
Trauner answered 10/5, 2016 at 10:55 Comment(0)
B
0

By default requestSerializer is set to an instance of AFHTTPRequestSerializer, which means that Content-Type of your request will be application/x-www-form-urlencoded.

But if your server requires application/json content type for that api, then you must set to an instance of AFJSONResponseSerializer
E.G.

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
 manager.requestSerializer = [AFJSONRequestSerializer serializer];
Bartz answered 7/5, 2016 at 19:13 Comment(0)
T
0

Encode your url.

NSString *unescaped = @"http://www";
NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];  
Tempe answered 9/5, 2016 at 4:57 Comment(0)
I
0

I had been encountering the same issue with AFNetworking POST as well as sometimes on GET calls but... Sometimes i would get a 500 internal server error although the server received request parameters!! Finally after some research with my web services backend developers, i came to know that its caused due to a DLL misconfiguration on the server side, particularly the System.reflection DLL file. Deleting the file from the server side (if it exists) removes the issue, otherwise copy pasting it back from the Bin (if it doesn't exist already) removes the issue. This is pretty baffling but apparently it fixes it!!

AND YES. IT HAS NOTHING TO DO WITH AFNETWORKING! The answer assumes Microsoft azure server

Innutrition answered 9/5, 2016 at 9:54 Comment(1)
Don't know how credible you want the source to be. Anyway, i think you are expecting the founders of AFNetworking to come and tell u it has nothing to do with them. And also you haven't bothered commenting to any of the answers which could obviously help understand us what really helped u and not. Also you didnt mention what server you are using etc..Innutrition
P
0

Try with formData

[manager POST:@"API" parameters:paramsDict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

} progress:^(NSProgress * _Nonnull uploadProgress) {

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

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

}];

it's AFNetworking 3.0 Method, but you can used same (formData) in AFNetworking 2.x

Pinelli answered 10/5, 2016 at 4:51 Comment(0)
J
0

Try to see what the detail accept/content type is for android. Do you know if they are using retrofit? Most likely it will have to do with your request or response serializer not matching what server is expecting.

Jabberwocky answered 11/5, 2016 at 20:57 Comment(0)
M
0

Add the below coding to AFURLResponseSerialization.m file and remove the existing code.

@implementation AFJSONResponseSerializer

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/xml",@"text/html", nil];

return self;
Midsection answered 30/1, 2017 at 4:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.