Can I make my OData request in JSON format? - ajax

I know OData supports responding in JSON format when it's given the appropriate Accept header:
Accept: application/json
Some articles say you'll need to specify odata verbosity otherwise you'll get the default xml format, but I have not seen this to be actually true. But let me mention it anyway:
Accept: application/json;odata=verbose
But (how) can I make my request using JSON instead of a querystring?

OData doesn't provide a way to specify the query in a request body, it only supports the query in the URL. So the answer is that there's no way to do that in JSON. Note that it applies to GET requests. Modification requests (POST/PUT/...) do accept JSON as the payload (typically representing an entity for example), in which case simply specify the content type of the request in its Content-Type header.
There are java script libraries which let you build the query string using more structured code (as compared to just strings). For example the datajs http://datajs.codeplex.com/.

Related

What is the "accept" part for?

When connecting to a website using Net::HTTP you can parse the URL and output each of the URL headers by using #.each_header. I understand what the encoding and the user agent and such means, but not what the "accept"=>["*/*"] part is. Is this the accepted payload? Or is it something else?
require 'net/http'
uri = URI('http://www.bible-history.com/subcat.php?id=2')
http://www.bible-history.com/subcat.php?id=2>
http_request = Net::HTTP::Get.new(uri)
http_request.each_header { |header| puts header }
# => {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"], "accept"=>["*/*"], "user-agent"=>["Ruby"], "host"=>["www.bible-history.com"]}
From https://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z3
This field contains a semicolon-separated list of representation schemes ( Content-Type metainformation values) which will be accepted in the response to this request.
Basically, it specifies what kinds of content you can read back. If you write an api client, you may only be interested in application/json, for example (and you couldn't care less about text/html).
In this case, your header would look like this:
Accept: application/json
And the app will know not to send any html your way.
Using the Accept header, the client can specify MIME types they are willing to accept for the requested URL. If the requested resource is e.g. available in multiple representations (e.g an image as PNG, JPG or SVG), the user agent can specify that they want the PNG version only. It is up to the server to honor this request.
In your example, the request header specifies that you are willing to accept any content type.
The header is defined in RFC 2616.

Hapijs avoid json validation when Content-type: application/json is present

I'm trying to receive a json payload on a hapijs server, that json may not be valid, since is some custom format that i need to manually proxy the request to an elasticsearch cluster, cannot use the proxy option for hapijs because i need to do multiple requests to different clusters, for that i use nodejs http library.
Elasticsearch doesn't receive a valid json when doing bulk actions, it receives new lines instead of commas, to separate json objects:
{"index":[".kibana-devnull"],"ignore_unavailable":true}
{"size":500,"sort":[]}
Hapijs tries to validate the json payload when it gets application/json header in the request and it responds "Invalid request payload JSON format", as i cannot remove that header i need to look for another method to allow that invalid json in the route, even if the header is present.
I would look at the docs, in particular http://hapijs.com/api#route-configuration. If you set payload.output.parse to false you will receive the raw buffer inside handler which can then be parsed by yourself as opposed to by the framework.

Does Elasticsearch support POST over GET only for the _search endpoint or all?

The official reference states that one can send _search requests also through POST instead of GET because not all clients support sending bodys with GET (see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html). You can then insert the query parameters from the URL also as JSON directly in the body.
Now I wonder: is this true for all GET requests that Elasticsearch offers that need query parameters?
For example, the _stat endpoint (https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html) is documented as a GET request (which makes sense), but supports URI parameters. Is it safe to use POST in this case as well and pass the parameters in the body using JSON?
No, the _search endpoint is one of a few special cases. If you look at the source code for the _stats endpoint in RestIndicesStatsAction.java, you can see that only the GET HTTP method is supported.
Using the POST method usually makes sense only when the payload to be sent can be substantially big, which is not the case for the few parameters such as the ones accepted by the _stats endpoint. In that case, sending those parameters in the query string is usually more than sufficient.

Web API ODataResult Is Always Serializing to Atom

I'm returning an ODataResult from the new Web API OData package as follows:
public ODataResult<Product> Get(ODataQueryOptions options)
{
var results = (options.ApplyTo(_db.Products) as IQueryable<Product>);
var count = results.Count;
var limitedResults = results.Take(100).ToArray();
return new ODataResult<Product>(results,null,count);
}
The above works great but it always returns an Atom response regardless of the query type. How can I return JSON?
I can see that Atom is the first supported media type within the ODataMediaTypeFormatter collection. I'd be happy just completely removing Atom support as I don't need it. Even better would be to have the content type correctly negotiated.
Have you tried setting the accept header, like this:
Accept=application/json;odata=verbose.
That should return JSON.
The OData protocol has supported JSON for a while but in V3 of the protocol application/json is mapped to something called JSON light (which is not yet implemented). So to until JSON light is supported to get JSON you have to be a little more specific, and request the more verbose version of JSON that has been in OData since V1.
I am currently also experimenting with OData support using the Beta Package you linked. We should keep in mind that it is a beta package and things might change until release. I have hit the same issue. Right now, it seems Microsoft is planning to provide OData v3 support in Web Api, looking at ODataMediaTypeFormatter reveals this.
In OData v3, there will be three formats:
Atom (application/atom+xml)
JSON Verbose (application/json;odata=verbose)
JSON Light (application/json;odata=light)
Atom is the only and thereby default XML format of OData. The default JSON Format in OData v3 is JSON light. The old OData v2 JSON Format is what has become JSON Verbose in OData v3.
This is supported by the Spec (v20120705) section 2.2.5.1 which lists Accept and corresponding Response Headers:
application/json;odata=verbose ->  application/json;odata=verbose
application/json;odata=light  -> application/json;odata=light
application/json -> For version 1.0 and version 2.0 responses: application/json;odata=verbose. For version 3.0 responses: application/json;odata=light
Unfortunately, they haven't specified JSON light yet (section 2.2.5.1.3, emphasis mine):
The JSON light format is a new format, defined only in the OData 3.0
protocol, that optimizes for small size by not including metadata
information in the representations. The verbose format represents the
existing JSON-based format, defined by the OData 1.0, 2.0 and 3.0
protocol, that includes metadata information along with the
representation of an instance. The format for the JSON light
content-type is not defined by this version of document.
I do expect however that the JSON Light format will be a proper subset of the JSON Verbose format. The current implementation of ODataMediaTypeFormatter will only respond to application/json;odata=verbose and application/atom+xml headers or fallback to ATOM. Interestingly enough, it seems specifying $format=json returns a 406.
If you want an interim solution that assumes the light format will be a proper subset of the verbose format, you can instruct the MediaTypeFormatter to respond to application/json requests, making it a lot easier to work with OData from jQuery because we can ommit specifying the accept header:
ODataMediaTypeFormatter odataFormatter = new ODataMediaTypeFormatter(model);
odataFormatter.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json"));

How do use JSON body in REST requests?

I am developing an API using Codeigniter and Phils RESTserver. I know how to send the request body in normal Form format but how can I send it as a JSON object instead?
I do this now:
lastname=bond
I want to do this instead:
{"lastname" : "bond"}
I tried to just replace the Content type header from:
application/x-www-form-urlencoded
In to this:
application/json
This did not do anything. Codeigniter says the POST array is empty.
If I understood correctly you want to create a request that contains a JSON node inside the request body. Assuming this, I think it's not possible to create such a request using simple HTML form tags as your browser always will try to pack your input vars in a querystring like format.
You will need JavaScript to achive this (I think all popular libs like Scriptacoulous or JQuery comes with helper methods for this).

Resources