REST API design: the endpoint which returns a report - api

I need to create an endpoint which return some form of a report. Something like:
api-v1/report?format=XML. And it report with custom XML-report.
What should I do in case xsl?
api-v1/report?format=XSL is it normal to answer on such request with XSL(Excel) file?

the resource (data) should be independent from the formatting/encoding
whether it is xml, json, xls, csv, etc should be determined through "content negotiation" usually accomplished by using the "accept" header.

One solution is to respond with the URL of where the file can be downloaded from, instead of sending the contents of the file.

Related

Pentaho rest client with variable url

I'm new to Pentaho and using the Rest Client. I can get the Rest client to work by using generate rows for the url. But then I need to pass part of the result of the json to be part of the url for the next request. I'm not sure how to do this. Any suggestions.
Remember that PDI works with streams, you, for each REST request you made, you will have one row as result. You will have as many rows as many requests you do.
I'm not sure if you can deserialize the JSON object directly from the PDI interface, but in the worst scenario, you can use the "User Defined Java Class" to use some external library (like Gson) and deserialize the object.
Then, you can create another variable in the UDJC step and concatenate the attributes you need on the URL string that comes from the last step.
In the other hand you can use "Modified Javascript" to deserialize it and return the attributes you need to then concatenate it with the URL. To use it, just declare varibles inside the code, and then use "Get variables" button to retrieve the available fields to send to the next step.
There are many ways to do it, I suggest you to use the Modified Javascript because it's easier to handle.
You CAN parse the Json response, just use Json Input a a nex step, and then use XPath to parse the field you want: $.result.the.thing.u.want.

Changing/appending request headers in RESTful API in c#

I have a really weird situation (may be its for me only). I developed a RESTful API. By default it returns the result as JSON/XML/TEXT as per the Content Type sent by the client in headers.
Now client is saying that he wants to set the response as default as XML only. What I mean here is that client will not send any content type in headers and it will by default send the request as XML.
When I access this API from browser, it return it as XML but when client's app requests it, it returns JSON result by default. They are getting the result as XML by putting the content type in headers but they don't want to do it and want to have XML result by default.
I hope I am clear on it. If not please let me know.
Any help would be appreciated. Thanks
[Change]
I am interested in knowing if there is some way I can modify the request headers when I receive request on server.
It is in MVC3, C#.
You can't change the request headers, just query them.
I guess you return your result as a simple string in your controllers, isn't it?
And, you are switching between results depending on the contenttype you read from request, don't you?
What is the contenttype the client call come with?
UPDATE:
Look at this page:
http://aleembawany.com/2009/03/27/aspnet-mvc-create-easy-rest-api-with-json-and-xml/
It's a solution for a previous version of MVC, but it will give you an idea about the solution you need:
Adjust the action result depending on the request contenttype
I find the answer and posting here. I just removed the other return types except the xml type like below:
void ConfigureApi(HttpConfiguration config)
{
// Remove the JSON formatter
config.Formatters.Remove(config.Formatters.JsonFormatter);
// or
// Remove the XML formatter
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
For more details, please follow below link
http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
Thanks

MVC 4 - Web Api and JSON?

I'm ramping up on MVC 4's Web API and I'm a bit confused about the default formatting. I want the API data to be in JSON. However, it's returning it in XML. According to the MVC 4 getting started video at http://www.asp.net/web-api/videos/getting-started/your-first-web-api, it should be JSON by default. But when I create a new Web Api project and run the sample, I get this:
<ArrayOfstring><string>value1</string><string>value2</string></ArrayOfstring>
I've been running around in circles trying to get this in JSON but apparently there is a lot of misinformation about this. Such as:
If I add "application/json" to the content type header, it should return JSON. This doesn't work, but I'm suspecting I don't have the header variable name right as I'm not finding the exact name to use. I've tried "Content-Type" and "contentType" in the request headers with no luck. Besides, I want JSON by default, not according to header info.
If I create a JsonFormatter and add it in Application_Start GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonNetFormatter(serializerSettings)); It should do the trick. But I gathered this depreciated as none of the examples are working.
What could I do, something simple preferably, to output data in JSON format by default?
Add this to GLOBAL.ASAX to get the response to be application/json
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
So it should look like this in context:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}
OR if you need to preserve XML as a media type you could instead edit App_Start/WebApiConfig.cs:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html") );
Which makes JSON the default response for a web browser but returns text/html.
Source
I want the API data to be in JSON. However, it's returning it in XML
How are you accessing your webapi? Are you using Chrome to access your webapi service (as Nick has mentioned in the comment)? Chrome adds the Accept header application/xml to the request...
If I add "application/json" to the content type header, it should return JSON
Try setting the 'Accept' header instead.
If I create a JsonFormatter and add it in Application_Start GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonNetFormatter(serializerSettings)); It should do the trick. But I gathered this depreciated as none of the examples are working.
If the accept header of the request is application/xml, content negotiation will still pick the XmlMediaTypeFormatter and will return application/xml. One more thing, the formatter for JSON is called JsonMediaTypeFormatter, and it is already in position 0 of the Formatters collection.
If you only want JSON then clear the formatters collection of all its defaults and then add just the JSON one back in.
You don't need to remove xml support to get JSON response. For GET requests, you should set the Accept-header - not the Content-Type header. For other HTTP verbs, it depends. Fully explained here.
http://blogs.msdn.com/b/kiranchalla/archive/2012/02/25/content-negotiation-in-asp-net-mvc4-web-api-beta-part-1.aspx
Bonus:
Use Google Chrome's Postman plugin to test REST APIs. Highly recommended :)
https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en
You can also write below in GLOBAL.ASAX
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
this also works for me.
Quoting Felipe Leusin (How do I get ASP.NET Web API to return JSON instead of XML using Chrome?)
I just add the following in App_Start/WebApiConfig.cs class in my MVC Web API project.
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html") );
That makes sure you get json on most queries, but you can get xml when you send text/xml.
If you need to have the response Content-Type as application/json please check Todd's answer below: https://stackoverflow.com/a/20556625/287145

How to set http headers in dotCMS

I'm trying to create a XML data feed with dotCMS. I can easily output the correct XML document structure in a .dot "page", but the http headers sent to the client are still saying that my page contains "text/html". How can I change them to "text/xml" or "application/xml"?
Apparently there's no way to do it using the administration console. The only way I found is to add this line of (velocity) code
$response.setHeader("Content-Type", "application/xml")
to the top of the page template.
Your solution is the easiest. However there are other options that are a bit more work, but that would prevent you from having to use velocity to do the XML generation, which is more robust most of the time.
DotCMS uses xstream to generate XML files (and vise versa). You could write a generic plugin to use this as well.
An JSONContentServlet exists in dotCMS that takes a query and generates json or xml (depending on your parameters). It is not mapped on a servlet by default, but that is easy to add.

Create request with SOAP ui

Is it possible to create soap ui request with data ? right now I know how to create new request which gives you template what you should send to your webservice (which is read from you wsdl), is there a way so your requests fills with data based on type(ex: String,Integer, Date) from wsdl, from soap ui or any other test tool ? thank you
In Soap UI 4.5.1 there's an option under preferences that once set will auto populate new requests with dummy data that is type safe instead of the default '?'.
File->Preferences->WSDL Settings-> Tick option "Sample Values" and hit OK.
You can enter data directly into the XML request generated by SoapUI, you can write scripts to populate data, or if you have the Pro version, it gives you a GUI form, based on the request, where you can simply type the data. i.e. it makes a data-entry screen for you.
The pro version can also pull data from databases, generate data from a string list, etc.. for example, I recently needed to send several thousand SOAP payloads with dummy data for firstname/lastname, etc.. So I made up two string lists, one with first names of past and present NFL quaterbacks, one with last names. I had the data generator in SoapUI (Pro) randomly select a string from each, and submitted it in a stress test for 1000 iterations. The results were very useful, yet very amusing, giving names like Drew Montana.
Yes, you can use SOAPUI to complete this task. you can fetch your data from several sources like Excel,plain csv file,Database liks DB2,POSTGRES. You can also write your groovy scripts to control the flow.Various things you can do to achieve this...