Django-tastypie. Output in JSON to the browser by default
Asked Answered
M

5

24

I see 'Sorry, not implemented yet. Please append "?format=json" to your URL.'. I need always append string "?format=json". Can I make a output in JSON by default?

Regards, Vitaliy

Metallophone answered 27/12, 2011 at 21:17 Comment(0)
R
41

From the tastypie cookbook, in order to change the default format, you need to override the determine_format() method on your ModelResource:

class MyResource(ModelResource):
    ....
    def determine_format(self, request):
        return 'application/json'

The above link demonstrates alternative methods of determining output format.

Also, I don't think a valid answer is essentially "You don't need this".

Edit

It appears GregM's answer is probably (I haven't tested it) the most correct with the new version of TastyPie, as per documentation putting the following in your settings.py will restrict the serialization output to json.

 TASTYPIE_DEFAULT_FORMATS = ['json']
Roussel answered 12/2, 2013 at 4:16 Comment(3)
TASTYPIE_DEFAULT_FORMATS rules!Sackett
What if you want JSON to be the default, but also to support XML if requested?Stylo
djangorestframework users visit : django-rest-framework.org/api-guide/settingsHindu
C
10

As of tastypie 0.9.13, if you do not need XML support you can disable it globally by setting TASTYPIE_DEFAULT_FORMATS to just ['json'] in your settings.py file. Requests should then default to JSON.

Cuticle answered 21/5, 2013 at 15:15 Comment(1)
One of those times where I wish I could give more than one ☝️ rec to a thing.Indoaryan
S
2

I've tested setting TASTYPIE_DEFAULT_FORMATS to ['json'] but it doesn't prevent the "Sorry not implemented yet" message when viewing the API from a browser.

I am able to make that warning go away by forcing the "Accept" header to 'application/json' in a middleware:

class TastyJSONMiddleware(object):
    """
    A Django middleware to make the Tastypie API always output in JSON format
    instead of telling browsers that they haven't yet implemented text/html or
    whatever.

    WARNING: This includes a hardcoded url path for /api/.  This is not 'DRY'
    because it means you have to edit two places if you ever move your API
    path.
    """

    api_prefix = '/api/'

    def process_request(self, request):
        if request.path.startswith(self.api_prefix):
            request.META['HTTP_ACCEPT'] = 'application/json'
Sapor answered 14/6, 2013 at 17:54 Comment(0)
V
1

To examine/test your REST API, use a Rest client instead of a browser, preferably one that knows how to pretty print JSON. I use the Postman plugin for Google Chrome.

If you want pretty json in command line:

curl https://api.twitter.com/1.1/search/tweets.json | python -m json.tool
Valli answered 2/1, 2012 at 11:40 Comment(0)
Z
1

Tasytpie has the defaults set as 'application/json'. But that is overridden by Browser request.

According to Tastypie, the default is overridden by Request Header ACCEPT and your format specification in GET ie. ?format=json. When you send request from browsers, if you see the Request HTTP Header sent, its something like -

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

The application/xml overrides the default in Tastypie Resource. Therefore, either you can set Browser Header to have 'application/json' (Bad idea) or you just specify in GET.

If you hit the same API url using CURL, you will see the JSON output without specifying that in GET.

Zweig answered 28/10, 2012 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.