ASP.NET WEB API 406 error:for POST request using Media format - asp.net-mvc

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

Related

Ruby on Rails basic use of RiotGames API (need explanation, solution already found)

First you must know I'm a total beginner, I'm trying to learn so I almost don't know anything.
On the basic page of the API, there is a curl command used as an example to show us how to make requests.
I'm using Ruby on Rails so I used "curl-to-ruby" website to translate it, but it did not work as expected.
I wanted it to show me this :
uri = URI.parse("REQUEST_URL")
response = JSON.parse(Net::HTTP.get(uri))
Instead I got this :
uri = URI.parse("REQUEST_URL")
response = Net:HTTP.get_response(uri)
I don't understand any of this, I thought I wouldn't need to and just use "curl-to-ruby", but apparently I really need to get this.
Would you please try to explain me ?
Or give me links ?
Or matters to read (curl, API, http) ?
Thank you very much, have a nice day.
It's because that command doesn't return just the content, it returns the whole HTTP response object including headers and body. You need to extract the response body and parse that using JSON.parse(), e.g.
JSON.parse(response.body)
See documentation here: https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-c-get_response
(Also, there is nothing in the cURL command which would hint to the converter that the content-type of the response was expected to be JSON (e.g. perhaps an "accepts" header or something), so even if it were able to produce extra code adding the JSON.parse part, it has no way of knowing that it would be appropriate to do so in this case.)

Is it possible to log the output of MarkupTemplateEngine in grails?

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();

Orbeon Form HTTP Service

Does anyone know how to pass parameters to a RESTFUL webservice using the Orbeon HTTP Service?
I have a RESTFUL API at http://localhost/RESTFUL/GETADDRESS/$parameter$.
Sample of the URL is http://localhost/RESTFUL/GETADDRESS/1234
Orbeon HTTP service is unable to pass the parameter to the web service.
The Request Body is configured as <parameter/> and serialization is set to XML.
Could not use HTML Form as it adds a ? to the URL which is not correct.
Anyone has any ideas to get this working?
There is no perfect solution. But try writing the service URL as:
http://localhost/RESTFUL/GETADDRESS/{...expression here...}
where "...expression here..." should be replaced by an XPath expression pointing to the value you would like to pass. For example, if pointing to a control called foo in a section called bar, try:
http://localhost/RESTFUL/GETADDRESS/{/*/bar/foo}
I also added this RFE.

Rails RESTful API: the proper error format when content negotiation fails

What error format must be used by RESTful API when content negotiation fails (ActionController::UnknownFormat is raised):
when a controller responds to the only one format (e. g. JSON) and the user has requested another one (e. g. XML), should the error be generated as JSON object or XML one?
when a controller responds to several formats and the user has requested neither of them, which one should be used during error generation: one of the 'known' by the controller or the one, having been requested by the user?
I think you are under no obligation to respond to any invalid request with the same format as the request was made. Imagine getting a request with payload in unknown binary format - what are you supposed to do in such a case.
ActionController::UnknownFormat should trigger a 406 Not Acceptable response, probably will in Rails 5.
You should rescue the ActionController::UnknownFormat and respond with proper HTTP code as well as set an Accept header listing all formats that your API supports (if Rails doesn't do it by default, I'm not sure)

Returning JSON format from ASP.NET web API

I know this question is been discussed many times but for me those solutions are not working. I want to return JSON data from my ASP.NET web API. I am hitting the end point using Firefox REST client plugin.
What I have tried:
I have specific accept header : Accept: application/json. Use accept header
Removed the XML formatter on Application_Start method
var formatters = GlobalConfiguration.Configuration.Formatters;
formatters.Remove(formatters.XmlFormatter);
This is how I return data at the end
return myModel.OrderBy(d => d.SortOrder);
Where myModel is just a class with few public property. I am not decorating this class or its property with any attribute.
But these two approach's are not working. I am still getting data in XML format :(
Please provide your suggestions.
I would like to introduce you to http://www.servicestack.net/
This is rest API framework that hooks up with .net.
It does everything what you require .
https://docs.google.com/presentation/d/16ey0MrpHOSz5N5sjctAliOgYWO3ZYeJe070fLZlPdrE/present#slide=id.i27
FROM COMMENT
Check that the application/json header is the first header and has a q=1 quality attribute: "application/json;q=1".
You can read more about quality attributes in the specs. Basically they are a way for a client of providing a preference system on the returned datatype.
To answer your other question (when I have explicitly removed the XML format in the code, why I was getting the data in XML format?), I can only guess what was going on: either the header was not setup correctly or the client was defaulting the other header to a different quality value.
Another guess could be that your way of removing the formatter is wrong: you can check this answer on SO or this article for alternative methods and see if they do the trick for you as well.

Resources