I am using groovys MarkupTemplateEngine to craft a xml like response for a calling application - to help in tracing issues I would like to log within my application the responses being provided to the calling app. Essentially i am looking to see if it is possible to log the generated template as returned to the client?
edit: So far I have tried using a response interceptor to access the response fields in both the after and afterview events with no luck
in log4j there is a special builder that could wrap your original writer and will write output into two destinations: log and original writer. (or it could be outputstream)
https://logging.apache.org/log4j/2.x/log4j-iostreams/apidocs/org/apache/logging/log4j/io/IoBuilder.html
Writer loggingWriter = IoBuilder.
forLogger(your_class_or_logger).
filter(original_writer).
buildWriter();
Related
Because I am rewriting a legacy app, I cannot change what the clients either send or accept. I have to accept and return JSON, HTML, and an in-house XML-like serialization.
They do, fortunately set headers that describe what they are sending and what they accept.
So right now, what I do is have a decoder module and an encoder module with methods that are basically if/elif/else chains. When a route is ready to process/return something, I call the decoder/encoder module with the python object and the header field, which returns the formatted object as a string and the route processes the result or returns Response().
I am wondering if there is a more Quart native way of doing this.
I'm also trying to figure out how to make this work with Quart-Schema. I see from the docs that one can do app.json_encoder = <class> and I suppose I could sub in a different processor there, but it seems application global, there's no way to set it based on what the client sends. Optimally, it would be great if I could just pass the results of a dynamically chosen parser to Quart-Schema and let it do it's thing on python objects.
Thoughts and suggestions welcome. Thanks!
You can write your own decorator like the quart-schema #validation_headers(). Inside the decorator, check the header for the Content-Type, parse it, and pass the parsed object to the func(...).
I am trying to upload a file using the rest assured framework. The call is a POST call on the API that i am using and the code is mentioned below:
given()
.contentType("image/jpg")
.accept("application/json")
.auth().oauth2(accessToken, OAuthSignature.QUERY_STRING)
.multiPart(new File("C:/Snap0000.jpg"))
.post("/objects/files")
.getBody();
The error that I get when doing this is:
400 - Unable to read image info Couldn't read magic numbers to guess format.
What am I doing wrong?
You may be missing the control name for the multipart? See javadoc for the multiPart method or look at the documentation.
Edit: above link is (partially) broken; this one at github seems to work better: https://github.com/rest-assured/rest-assured/wiki/Usage#multi-part-form-data
I am very new to web api stuff:
I am getting an error
406: Not Acceptable
error message in asp.net web api rest service.
In my rest service I’m using media format for my customized XML output, to get customized output.
I’m registering my formatted media in Global.asax page.
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new mynewformat());
all my methods are post methods with typed object as parameter and parameters are accepts from body.
Whenever I try to test the service… Getting 406: Not acceptable error message.
can anyone please help me ... what could be the reason for this....???
I did notice couple of interesting points here...
If I’m commenting below line then I’m getting 200 (OK) status code (which is fine.)... but format is not applying to output.
GlobalConfiguration.Configuration.Formatters.Clear();
If i'm removing parameters in my service method.. Then its working
fine..
I request everyone.. Please guide me what could be the reason/work around/solution/fix..for this issue.
Note:I don't want accept parameters from URI so i made it to accept from frombody only.
Thanks.
There is a lot more to implementing a custom format than just adding it to the configuration formatters. It starts with having to change the media-type header to a new custom type of your choosing (like "application/myNewFormat") for all requests, for the client. On the back end, you have to implement a new MediaTypeFormatter that can handle the serialization. This involves a bit more of code.
A good example of this resides here, it can easily be stripped to boiler-plate code:
http://www.codeproject.com/Articles/559378/Implementing-Custom-Media-Formatters-in-ASP-NET-We
I notice OpenRasta.Core has an HtmlErrorCodec which is responsible for rendering a the server error page sent out when a handler throws an Exception.
When I make an JSON Ajax request to an exception throwing handler this Codec is selected and the exception is rendered as HTML.
I have tried to register my own IMediaTypeWriter for IList<Error> with MediaType("application/json") so I can send back JSON to the browser, but it seems to be ignored. Can anyone help?
Thanks
Neil
If there is an error, indeed a codec with IList will be selected, but will follow the normal conneg for a type.
I'd suggest having a look at the request log and finding out how and why the html codec gets selected (I'd suspect with my remote debugging tunnel vision that you may have a browser sending the equivalent of Accept: text/html,application/json, at which point OR doesn't really know which of the two is acceptable, which is probably a bug as we register text/html with a q of 1 where it should be 0.5). If that's indeed what the problem is, the solution is to remove the registration for the html error codec, which you can do by providing your own DependencyRegistrar.
Can you just catch your exceptions, wrap them in a type and do something like:
ResourceSpace.Has.ResourcesOfType<MyErrorWrapper>().WithoutUri.AsJsonDataContract()
I'm looking for a rails solution that can consume multiple remote XML services, passing dynamic request parameters and outputting the response as XML or JSON.
I've looked into TinyProxy (Can't get it to install on OSX via macports) and also Nginx. Nginx looks like it will do what I need and also give us flexibility going forward with load balancing etc.
Has anyone else got any experience of this? Any tried and tested solutions?
In stead of going through a proxy, one of the standard solutions around the same-origin policy is dynamic script tags and JSON callbacks.
For example: you page page wants to query an API at remotesite.com and you try to do an ajax call to http://remotesite.com/api?query=list but you get the same-origin error. To circumvent the restriction you could add a script tag to the DOM (using JS) that points to the url like this:
var e = document.createElement('script');
e.src = 'http://remotesite.com/api?query=list';
document.getElementById('fb-root').appendChild(e);
The browser would then run that request - the same thing you tried to do w/ an ajax call. Now the catch is that you need to have the response call one of your js functions w/ the data returned as a argument. So the request would return something like:
callbackFunctionname({...json_data_here...});
Now in your code you'd have a function like this:
function callbackFunctionname(json_string)
{
//you have result from cross domain ajax request.
}