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.
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.
Related
As of today (September 13, 2017) did Bing Image Search v 5.0 change their backend API?
Using this console, adding the header "Content-Type" with a value of "multipart/form-data" gives a response with expected values.
However, when called from my iOS app with the exact same parameters and headers, I receive an error response of "RequestParameterInvalidValue;
message = "Parameter has invalid value.";
parameter = imgurl;"
The following code worked as of yesterday:
NSString* path = #"https://api.cognitive.microsoft.com/bing/v5.0/images/search";
NSString* skip = [NSString stringWithFormat:#"skip=%li", (long)searchOffset];
NSString* queryString = [NSString stringWithFormat:#"q=%#", searchQuery];
NSArray* array = #[
// Request parameters
#"entities=true",
#"count=50",
skip,
#"safeSearch=Strict",
queryString
];
NSString* string = [array componentsJoinedByString:#"&"];
path = [path stringByAppendingFormat:#"?%#", string];
NSLog(#"%#", path);
NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
[_request setHTTPMethod:#"POST"];
// Request headers
[_request setValue:#"multipart/form-data" forHTTPHeaderField:#"Content-Type"];
[_request setValue:accessKey forHTTPHeaderField:#"Ocp-Apim-Subscription-Key"];
// Request body
[_request setHTTPBody:[path dataUsingEncoding:NSUTF8StringEncoding]];
I can remove the Content-Type header from the code in Test and get a working response, but this worked yesterday on our Production app. Is there a workaround to get my production code working again without having to resubmit the App for approval?
There is a fix in deployment now that should fix issue with POST requests.
Also had this error right now. Tried to debug it and this is what my dump returned:
HTTP_Request2_Response {#460 ▼
#version: "1.1"
#code: 400
#reasonPhrase: "Bad Request"
#effectiveUrl:
"https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=cats"
#headers: array:16 [▼
"cache-control" => "no-cache, no-store, must-revalidate"
"pragma" => "no-cache"
"content-length" => "169"
"content-type" => "application/json; charset=utf-8"
"expires" => "-1"
"vary" => "Accept-Encoding"
"server" => "Microsoft-HTTPAPI/2.0"
"p3p" => "CP="NON UNI COM NAV STA LOC CURa DEVa PSAa PSDa OUR IND""
"bingapis-traceid" => "18AAD6F029D1439EB653971FBD07B6EF"
"x-msedge-clientid" => "260206484A6764FE375C0CB54B826518"
"x-msapi-userstate" => "d3dd"
"x-msedge-ref" => "Ref A: 18AAD6F029D1439EB653971FBD07B6EF Ref B:
SG2EDGE0713 Ref C: 2017-09-14T07:12:07Z"
"apim-request-id" => "9de68803-fee7-4a56-aa0e-f00e2b43929b"
"strict-transport-security" => "max-age=31536000; includeSubDomains;
preload"
"x-content-type-options" => "nosniff"
"date" => "Thu, 14 Sep 2017 07:12:06 GMT"
]
#cookies: []
#lastHeader: "date"
#body: "{"_type": "ErrorResponse", "instrumentation": {}, "errors":
[{"code": "RequestParameterInvalidValue", "message": "Parameter has
invalid value.", "parameter": "i ▶"
#bodyEncoded: true
I thought there was something wrong with my code but then I saw your thread here and concluded it was an error on Bing/Azure's end.
The following curl works. However, yesterday I completely redid my Microsoft cognitive keys in all categories because they became broken. I thought the keys probably broke months ago but maybe it was just yesterday. I did not have to change any parameters in any of my many test files.
curl "https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=cats" -H "Content-Type: multipart/form-data" -H "Ocp-Apim-Subscription-Key: <My Bing Search API Key 1>"
I can't figure out why this message is being thrown by AFNetworking 2.0. My understanding is that application/json is the default serialization scheme, so if the server returns JSON with content-type:application/json, why would AFNetworking throw this error?
Failure with messages Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: application/json" UserInfo=0x17e2ed60 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x17e84100> { URL: http://XXX/XXX/XXX } { status code: 200, headers {
Connection = "keep-alive";
"Content-Type" = "application/json;charset=UTF-8";
Date = "Fri, 14 Aug 2015 16:16:52 GMT";
Server = "Apache-Coyote/1.1";
"Transfer-Encoding" = Identity;
"X-Application-Context" = application;
} }, NSErrorFailingURLKey=http://XXX/XXX/XXX, NSLocalizedDescription=Request failed: unacceptable content-type: application/json, com.alamofire.serialization.response.error.data=<7b227374 61747573 223a3230 302c2263 6f646522 3a225636 594d227d>}
Commented out URL's with X's in the code block. This only happens sometimes, and it causes the request to fail. If I issue another request, it succeeds.
I'm using an AFHTTPSessionManager singleton to issue all requests.
Try this:
sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript", #"text/html", nil];
I send a post request with password and login data to the server.
I need verify that username and password is correct.
With curl I check 2 Fields in Header: "Set-Cookie" and "Location".
Response with correct login data:
Vitaliys-MacBook:~ aivs$ curl -d "act=login&login=19276&pass=abcdif" https_my_server/zboxweb --header "Content-Type:application/x-www-form-urlencoded" --dump-header /dev/stdout
HTTP/1.1 302 Found
Server: nginx
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-cache
Expires: 01 Dec 1980 00:00:00 +0400
Set-Cookie: ZBW_SESSID=34523cf45445e0072f0555692cdea2a9e3fdd308; path=/; expires=Wed, 23-Jul-2014 10:32:53 GMT
Date: Wed, 23 Jul 2014 10:17:53 GMT
Location: https_my_server:443/
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, OPTIONS
Response with incorrect login data:
Vitaliys-MacBook:~ aivs$ curl -d "act=login&login=10286&pass=1234567" https_my_server/zboxweb --header "Content-Type:application/x-www-form-urlencoded" --dump-header /dev/stdout
HTTP/1.1 302 Found
Server: nginx
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-cache
Expires: 01 Dec 1980 00:00:00 +0400
Set-Cookie: ZBW_SESSID=; path=/; expires=Wed, 23-Jul-2014 10:18:19 GMT
Date: Wed, 23 Jul 2014 10:18:19 GMT
Location: https_my_server:443/zboxweb?err=bad_login
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, OPTIONS
With "Set-Cookie" and "Location" fields I can see that the login data is correct or incorrect
AFNetworking 2.3.1 doesn't show these fields!
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager POST:#"https://find.zwave.me/zboxweb/"
parameters:#{#"act": #"login", #"login": #"19276", #"pass" : #"abcdif "}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Header: %#",operation.response.allHeaderFields);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
AFNetworking Response with incorrect login data:
2014-07-23 15:14:23.911 Z-Way Tablet APIv1[22666:60b] Header: {
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Methods" = "GET, POST, OPTIONS";
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "no-cache";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 23 Jul 2014 11:12:13 GMT";
Expires = "Wed, 23 Jul 2014 11:12:13 GMT";
Server = nginx;
"Set-Cookie" = "ZBW_SESSID=; path=/; expires=Wed, 23-Jul-2014 11:12:13 GMT";
"Transfer-Encoding" = Identity;
}
In response with incorrect login data "Set-Cookie" field is exist
AFNetworking Response with correct login data:
2014-07-23 15:57:00.268 Z-Way Tablet APIv1[22738:60b] Header: {
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Methods" = "GET, POST, OPTIONS";
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html";
Date = "Tue, 22 Jul 2014 09:21:21 GMT";
Etag = "\"53a152fb.4288\"";
"Last-Modified" = "Wed, 18 Jun 2014 08:51:07 GMT";
Server = nginx;
"Transfer-Encoding" = Identity;
}
In response with correct login data "Set-Cookie" and "Location" fields isn't exist
How to use AFNetworking 2 to get "Set-Cookie" and "Location" fields?
Am trying to use AFNetworking 2's AFHTTPSessionManager to post pdf content to my web service. I have subclassed AFHTTPSessionManager and set request and response serializers to the corresponding AFJsonXXXSerializer. However when I POST the form data as a multi part request, I get a JsonParseException. Am pretty sure this is a straight forward use case and I might be missing something. Pls help, Thanks in advance !
[accessMgr postDataToURL:#"objects-d2" usingParams:nil fileData:imageData andDelegate:self];
which calls the below method to POST
-(void) postDataToURL:(NSString *) urlString usingParams:(id)parameters fileData: (NSData *)fileData andDelegate:(id<RemoteAccessDelegate>) remoteDelegate
{
//RESTSessionManager extends AFHTTPSessionMaanger
RESTSessionManager *manager = [self getManager:remoteDelegate];
[manager POST:urlString parameters: parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:fileData name:#"content" fileName:#"test.pdf" mimeType:#"application/pdf"];
The request goes with the following headers and the form data
POST /d2fs/repositories/ls67sp2/objects-d2 HTTP/1.1
Host: localhost:8080
Content-Type: multipart/form-data; boundary=Boundary+422AF86226B68040
Connection: keep-alive
Transfer-Encoding: Chunked
Accept: application/json
User-Agent: IIG Mobile/1.1 (iPad Simulator; iOS 7.1; Scale/2.00)
Accept-Language: en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5
Authorization: Basic bHNhZG1pbjpsc2FkbWlu
Accept-Encoding: gzip, deflate
--Boundary+422AF86226B68040
Content-Disposition: form-data; name="content"; filename="test.pdf"
Content-Type: application/pdf
%PDF-1.3
%ƒÂÚÂÎßÛ†–ƒ∆
<more file content>
I have verified that the trailing boundary is also set. The response am getting from my web service is
{
"status": 400,
"code": "E_INPUT_ILLEGAL_ARGUMENTS",
"message": "There are illegal arguments provided.",
"details": "org.codehaus.jackson.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.commons.fileupload.MultipartStream$ItemInputStream#22a5cdce; line: 1, column: 2];Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.commons.fileupload.MultipartStream$ItemInputStream#22a5cdce; line: 1, column: 2]"
}
I was able to resolve this issue today. I had to append a json object which was metadata about the object before appending the file data and the upload worked as a charm ! Thanks.
[formData appendPartWithFormData:data name:#"object"];
[formData appendPartWithFileData:fileData name:#"content" fileName:#"test.pdf" mimeType:#"application/pdf"];
I got the Google API for Objective C for using Cloud Storage Module from below path.
Google API Objective-C Client
Unluckily the api didn't provide any Sample Code for Cloud Storage so I tried to do it myself but I couldn't be successful. Below is what I am doing
I enabled my billing for Cloud Storage from Google API Console
I made a bucket with name "ahs_test"
I made a Client ID for installed applications
I got successful with Outh2.0 with the library available at upper SVN Path. After doing this I wrote below code and get below error message
Note that After reading Google Cloud Storage I am sort of sure that I have to send the "x-goog-project-id" in my request header but I am wondering that this API's code doesn't do anything like that. ( I might be doing some mistake so leaving this for getting any sort of help.. Thanks in Advance...)
// Code....
GTLServiceStorage *service = self.storageService
GTLQueryStorage *query = [GTLQueryStorage queryForBucketsGetWithBucket:#"ahs_test"];
_fileListTicket = [service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLStorageBuckets *bucketList,
NSError *error) {
}];
// Error Message I get (Detailed from loger)
storage.buckets.get
2012-12-30 07:11:30 +0000
Request: POST https://www.googleapis.com/rpc?prettyPrint=false
Request headers:
Accept: application/json-rpc
Authorization: Bearer _snip_
Cache-Control: no-cache
Content-Type: application/json-rpc; charset=utf-8
User-Agent: com.example.DriveSample/1.0 google-api-objc-client/2.0 MacOSX/10.8 (gzip)
Request body: (128 bytes)
{
"jsonrpc" : "2.0",
"method" : "storage.buckets.get",
"id" : "gtl_3",
"params" : {
"bucket" : "ahs_test",
"max-results" : 150
},
"apiVersion" : "v1beta1"
}
Response: status 200
Response headers:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Encoding: gzip
Content-Length: 132
Content-Type: application/json; charset=UTF-8
Date: Sun, 30 Dec 2012 07:10:44 GMT
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Pragma: no-cache
Server: GSE
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Response body: (168 bytes)
{
"error" : {
"message" : "Access Not Configured",
"data" : [
{
"reason" : "accessNotConfigured",
"message" : "Access Not Configured",
"domain" : "usageLimits"
}
],
"code" : 403
},
"id" : "gtl_3"
}
The Google API library uses the Cloud Storage JSON API, which isn't enabled by default. Please check to see if it is enabled in the Google APIs console.