Let me start by saying I know the problem is the backend is the response, but there is nothing I can do about it. So...
I have a GET request that, on some cases, will send back a 200 response with an empty body. Again, can't change it.
Using Restkit, I have fairly every possible case properly mapped and everything works like a charm, but with this specific case I can't get it to be mapped as it should.
From everything I've seen, this seems to be the most accurate option to what I'm trying to do.
But, for some reason, the response descriptor doesn't seem to be picked up and the response is treated as a failure with the following error:
Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (200) with content type 'application/json'"
Is there an 'easy' way to properly map an empty response, or even a string as response (clearly with no JSON structure whatsoever) ?
Thanks in advance for any light you can shed in my direction.
As described in this post, there is an even better solution to your problem.
You can simply add an ResponseDescriptor with an ObjectMapping to NSNull:
// Create RKObjectMapping for NSNull:
RKObjectMapping* responseMapping = [RKObjectMapping mappingForClass:[NSNull class]];
// Create RKResponseDescriptor with mapping:
RKResponseDescriptor* response = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
method:RKRequestMethodAny
pathPattern:#"/yourPathPattern"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
// Add RKResponseDescriptor to RKObjectManager:
[[RKObjectManager sharedManager] addResponseDescriptor:response];
You should not have 2 response descriptors in this case, you should only have one. That one response descriptor is linked to a dynamic mapping which analyses the response and returns the appropriate mapping (from your 2 existing mappings).
Related
My code is working ok for GET/POST/PUT to/from restApi1 and restApi2.
However, my problem I need to implement HEAD/OPTIONS (no body!) and GET uri1
HEAD/OPTIONS could return 204 or 200 depends on a process status. I am getting error "Stream closed". Sounds like Camel want body bytes, but I don't indend to have it. Even I set ExchangePattern.InOnly or optional etc error occur...
What is correct way to see responses and handle requests WITHOUT body, just statuses exchange?
How to see response from restApi2 on Camel rest("/restApi1").head().route().routeId("id1")
.to("direct:restApi2").routeId("/id1").setHeader(Exchange.HTTP_METHOD,constant("HEAD"))
setExchanggePattern(ExchangePattern.OutOptionalIn).recepientList(simple(restApi2));
I figured it out. Need to set '.convertBodyTo(String.class)' even I don't have a body.
I have the following CQL apoc query in neo4j that works fine to get response payload from a rest GET request:
CALL apoc.load.jsonParams($uri, {Authorization: $Bearertoken}, null)
YIELD value
UNWIND value.items AS item
RETURN item
However, the uri uses pagenation and the next page uri is present in the response header. Therefore, I need a way to retrieve the response header along with the value. Please let me know if there is a way to do this.
Thanks in advance.
The apoc.load.jsonParams procedure does not support returning the response header.
You can write your own code instead. Here is an article that can help get you started.
I use ResponseObjectSerializable like described here:
https://github.com/Alamofire/Alamofire#generic-response-object-serialization
And i want to validate if the status code is in a range
https://github.com/Alamofire/Alamofire#validation
My call looks like this:
Alamofire.request(Router.Something())
.validate(statusCode: 200..<300)
.responseObject { (request, response, object:Object?, error) in
println(object)
println(request)
println(response)
}
My Problem is if the validation fails responseObject anyhow get called and try to serialize the empty response.
How I can handle that without validate the response a second time in my ResponseObjectSerializable?
That's a really good question.
The long story short is that you can't. Your responseObject serializer is not notified about a validation error. It only receives the request, response and data objects and needs to attempt to construct the object from the data.
The ResponseObjectSerializable link you posted does exactly that. If the serialization succeeds, it will return a valid object. If it fails, it will return a nil object and a serialization error.
Where it gets interesting is if you return a serialization error, but the validation also failed. In that case, your completionHandler will actually be called with a nil object, and the validation error, not the serialization error. Alamofire prioritizes the validation error over the serialization error if the validation is run before the responseObject.
As a sidenote, your responseObject serializer should handle the data coming back from the server safely, regardless of the status code that was returned. If parsing the data fails, your serializer should return a serialization error. If it succeeds, then return the object.
I'm using AFNetworking 2.0 for a normal GET request and the parameter dictionary seems to be having no effect. I'm using the standard:
[manager GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {...
but my NSMutableDictionary (parameters) doesn't seem to have any effect on the URL sent in the GET request (GET requests don't have HTTP Bodies as far as I'm aware so they won't be there). So where are the parameters going? I know I could always amend the string, but the way in which I receive the parameters on an optional basis makes it far easier to just send in a dictionary. What's wrong, why isn't it working?
Thanks,
Mike
UPDATE: I'm setting the parameter like this: [parameters setValue:self.Object.ID forKey:#"Filter.Id"]; ID is a string. However, when I do [parameters valueForKey:#"Filter.Id"] I get a (null) in the NSLog. I'm going to mess around with it a little and see how it goes, but if you have any ideas why this is happening I'd appreciate it. Also, parameters is an NSMutableDictionary and is a property of the ViewController.
Was an issue with my NSMutableDictionary property. Changed it to a global variable and everything worked fine!
Thanks,Mike
How can I make a request that contains no headers fields? The requests are being sent to my own server implementation from scratch, which doesn't care about header fields. The request will at most contain only a post body. Let me know if I'm missing something logical.
Please don't tell me about ASIHTTPRequest. Thank you.
As I wanted to remove specific header on a NSMutableURLRequest, I've just found that calling setValue:forHTTPHeaderField: with a nil value actually removes it.
It's not documented by Apple, but it seems quite logical.
For me the code
[request addValue:#"" forHTTPHeaderField:#"User-Agent"];
only added an empty string instead of cleaning the User-Agent.
Instead, using setValue fixed it:
[request setValue:#"" forHTTPHeaderField:#"User-Agent"];
I found that for some of the header fields ("User-Agent" is one of them), setting the header value to nil using
[request addValue:nil forHTTPHeaderField:#"User-Agent"];
doesn't actually remove the header field, but rather sets it to a default value!
If you want to actually remove the content, it is enough setting the value to an empty string with
[request addValue:#"" forHTTPHeaderField:#"User-Agent"];
Why not just ignore them, if you control the server implementation?
Does [request setAllHTTPHeaderFields:[NSDictionary dictionary]] work?
If #2 didn't work, try making your own subclass that always returns an empty dictionary from the allHTTPHeaderFields method, and nil from the valueForHTTPHeaderField: method. But NSURLConnection might make a copy of your request, so you might have to override copyWithZone: also.