Solution to IOS problem POST network request status code: 500

  • 2021-10-16 05:07:14
  • OfStack

Solution to IOS problem POST network request status code: 500

Foreword:

iOS 10 uses [NSURLSession uploadTaskWithRequest: request fromData: jsondata completionHandler: ^ NSData * _ Nullable data, NSURLResponse * _ Nullable response, NSError * _ ES22error) {}];

Error 500 while making post network request

Error 500 The server encountered an unexpected condition that prevented it from completing the processing of the request. Generally speaking, this problem will occur when the program code of the server goes wrong. Summary of Common Status Codes for IOS HTTP Requests

Although the reason for this is a server error, I think it is possible for the server to parse the request data, so I started with the customer service side.
The value in response when printed back is


 : <NSHTTPURLResponse: 0x17003a900> { URL:  Yours url Address  } { status code: 500, 

headers {
 "Cache-Control" = private
 "Content-Length" = 3306;
 "Content-Type" = "text/html; charset=utf-8";
 Date = "Sun, 09 Oct 2016 07:45:13 GMT";
 Server = "Microsoft-IIS/7.5";
 "X-AspNet-Version" = "4.0.30319";
 "X-Powered-By" = "ASP.NET";
} }

From this we can see that the return type is not json but text/html type

As we know, in post, the request header data type needs to be set. I set the above error request like this


[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

This setting method is copied from the network request method for submitting pictures, that is, @ "application/x-www-form-urlencoded" is the type setting for submitting pictures (when submitting pictures, change the submission picture parameters to NSData and then to NSData submission of base64). My request this time is to submit the NSData type of json string. Therefore, the value of "Content-Type" can be set to "application/json". Explanation of Content-Type in Http request and its application in Spring MVC

My solution is to reset it as follows:


[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 

Request response, you can compare it with the above.


<NSHTTPURLResponse: 0x17403fe60> { URL:  Yours url Address  } { status code: 200, headers {
 "Cache-Control" = private;
 "Content-Length" = 75;
 "Content-Type" = "application/json; charset=utf-8";
 Date = "Sun, 09 Oct 2016 07:56:16 GMT";
 Server = "Microsoft-IIS/7.5";
 "X-AspNet-Version" = "4.0.30319";
 "X-AspNetMvc-Version" = "4.0";
 "X-Powered-By" = "ASP.NET";
} } 

The current status code: 200 solves the problem of 500 errors. It is also possible that the background handles the data incorrectly, or the data type returned by the background conflicts with the type returned by the front-end settings. Or have to analyze specific problems

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: