0

I have 2 projects one for user and the other one for provider.

I use the same code and sam api for an activity, and it works fine in provider but it does not work for user!!

I used this code

AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    NSString *strURL=[NSString stringWithFormat:@"%@%@",SERVICE_URL,path];

manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

but the error is:

Error Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: forbidden (403)" UserInfo={NSLocalizedDescription=Request failed: forbidden (403), NSUnderlyingError=0x600002a08ba0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={NSLocalizedDescription=Request failed: unacceptable content-type: text/html, NSErrorFailingURLKey=https://api.com, com.alamofire.serialization.response.error.data={length = 3273, bytes = 0x3c21444f 43545950 45206874 6d6c3e0a ... 3c2f6874 6d6c3e0a }, com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x6000024d0b80> { URL: https://api.com } { Status Code: 403, Headers {
    "Cache-Control" =     (
        "private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
    );
    "Content-Encoding" =     (
        br
    );
    "Content-Type" =     (
        "text/html; charset=UTF-8"
    );
    Date =     (
        "Thu, 25 Feb 2021 07:26:49 GMT"
    );
    Expires =     (
        "Thu, 01 Jan 1970 00:00:01 GMT"
    );

I added this line in the code:

manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

but this error appears:

JSON text did not start with array or object and option to allow fragments not set.

then, i added this code:

manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];

and it gave me the same issue for accepting text/html !!!

give me a solution please.

thank you in advance.

hawra
  • 1
  • I note that you got a `Request failed: forbidden (403)`, I think the issue is more on the 403, no? Can you retrieve the response data and convert it into String? – Larme Feb 25 '21 at 08:17
  • @Larme the response data is always null ! and the function returns error and failure. what Request failed: forbidden (403) is for? – hawra Feb 25 '21 at 08:22
  • Try to retrieve the response body in case of error with https://stackoverflow.com/questions/35083273/get-responseobject-on-failure-block-afnetworking-3-0 it should give info. For the rest, read about HTTP 403 error code. – Larme Feb 25 '21 at 08:57
  • @Larme it gave me an empty NSLog! nothing shows – hawra Feb 25 '21 at 09:30
  • I see the info `com.alamofire.serialization.response.error.data={length = 3273, bytes = 0x3c21444f 43545950 45206874 6d6c3e0a ... 3c2f6874 6d6c3e0a`, try to get that data. So I'd say: `NSData *errorData = error.userInfo[@"com.alamofire.serialization.response.error.data"]`, if not nil, use then https://stackoverflow.com/questions/1305225/best-way-to-serialize-an-nsdata-into-a-hexadeximal-string to convert it into hex string. – Larme Feb 25 '21 at 09:38
  • From the bit I got `3c21444f 43545950 45206874 6d6c3e0a` and `3c2f6874 6d6c3e0a`, I got ` \n` and ` – Larme Feb 25 '21 at 09:42
  • As a general recommendation, I recommend that you use the Bagel utility for debugging. Because it is quite possible that you send a request with some other parameters, and just do not see it. https://github.com/yagiz/Bagel – I.U. Feb 25 '21 at 10:03
  • I used this code: `NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding]; NSLog(@"%@",ErrorResponse);` and the error shows that the access for the server is denied!! Thank you @Larme – hawra Feb 25 '21 at 12:02

1 Answers1

0

It fixed !!

I tried to print the error using this code:

NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];

NSLog(@"%@",ErrorResponse);

and the error was: Access denied | api.com used Cloudflare to restrict access

The owner of this website (api.com) has banned your access based on your browser's signature

It is a server security issue.

hawra
  • 1