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

Michael A
  • 8,239
  • 20
  • 66
  • 107
moskrc
  • 1,198
  • 1
  • 11
  • 22

5 Answers5

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']
mrmagooey
  • 4,493
  • 6
  • 34
  • 47
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.

GregM
  • 1,508
  • 1
  • 11
  • 18
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'
btubbs
  • 1,725
  • 15
  • 19
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.

Ravi Kumar
  • 1,344
  • 14
  • 21
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
Mohammed H
  • 6,326
  • 12
  • 72
  • 119