OData $ format system request option calls Bad Request 400

I have a very simple reflex based OData example that works fine and generates json when I use the Accept header as indicated. However, I cannot get it to work with the parameter $ format = json. Whenever I add this parameter, I get a Bad Request. Accordingly, it looks like it should work: link text

Please note that other system request parameters, such as $ select, work fine. This .Net 4 works through VS2010.

+4
source share
2 answers

Using $format=json out of the box against .NET 4 WCF Data Service will not work even if the OData Spec says it is supported. I don’t know exactly why Microsoft does not directly support it. But in this situation, there are two workarounds: the person feels a little hacked, and the other makes sense.

First, a solution that is a bit hacked is to create an HttpHandler that intercepts your request, reads the querystring $format=json parameter, and then adds the accepts header to your request (when removing the offensive $format=json parameter). This is described in this blog post .

The second solution, which sounds a little better, is to decorate your data service with the [JSONPSupportBehavior] attribute. This makes a little more sense and is a little easier to implement (since you don't need to create an HttpHandler). Here are some useful links:

  • Blog post describing how to use it.
  • Link to download the source code for the [JSONPSupportBehavior] attribute (yes, you have to build it - I did not find the compiled download).

I like the attribute approach, I'm just sorry it wasn't loading from CodePlex ... it just doesn't sound that supported. But this is just my opinion.

Honestly, if you have control, the best approach is to simply add the accepts header to your application/json request and your service will automatically return formatted JSON results.

Hope this helps.

+4
source

Anyone who comes across this ... Now you can use the Data Services Toolkit and inherit from ODataService, not DataService, to automatically enable this feature.

0
source

All Articles