Tastypie: return html content in API response

r.bhardwaj

I need to create an API that takes application/x-www-form-urlencoded data in POST data and returns a HTML content in response.

Now to serialize the urlencoded data I am calling following serializer from resource :

class urlencodeSerializer(Serializer):
formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'urlencode']
content_types = {
    'json': 'application/json',
    'jsonp': 'text/javascript',
    'xml': 'application/xml',
    'yaml': 'text/yaml',
    'html': 'text/html',
    'plist': 'application/x-plist',
    'urlencode': 'application/x-www-form-urlencoded',
    }
def from_urlencode(self, data,options=None):
    """ handles basic formencoded url posts """
    qs = dict((k, v if len(v)>1 else v[0] )
    for k, v in urlparse.parse_qs(data).iteritems())
    return qs

def to_urlencode(self,content):
    pass

To specify the response format I have added this function in the resource :

    def determine_format(self, request):
        return 'text/html'

Now as I am trying to output the HTML response as below :

data = "<html><h1>Hello</h1></html>"
return HttpResponse(data, content_type='text/html', status=200)

I am getting following error :

Sorry, not implemented yet. Please append "?format=json" to your URL.

Can anybody suggest me what is wrong with this code and how to achieve the given requirement.

r.bhardwaj

I have found the answer though not sure if its the correct way to do in tastypie :

  1. Normally return the bundle response from resource.

  2. Add following method in urlencodeSerializer

    def to_html(self,bundle, options):
         return prepare_html_from_bundle_data(bundle.data)
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Web API POST MultipartFormDataContent: Can response return multipartform content?

From Dev

Web API POST MultipartFormDataContent: Can response return multipartform content?

From Dev

Tastypie API return only one object for given parameters

From Dev

PHP bing api html content translation response displays as plain text

From Dev

PHP bing api html content translation response displays as plain text

From Dev

Wikipedia API doesn't return the same content that is in HTML

From Dev

Content negotiation to return HTML

From Dev

Tastypie api for multiple models

From Java

Return content with IHttpActionResult for non-OK response

From Dev

Get the content HTML of an API

From Dev

Exclude a field from tastypie api

From Dev

Django Tastypie Postgres slow server response

From Dev

tastypie overid create_response() method

From Dev

Setting the charset of html response content to 1252

From Dev

How to access _content from a api response in python

From Dev

Return html response to calling function in nodejs

From Dev

Is it possible to return a response from a Web API constructor?

From Java

Return response from API as inner JSON objects

From Dev

Web API AuthorizeAttribute does not return custom response

From Dev

Web API AuthorizeAttribute does not return custom response

From Dev

Return user_id as response of post api

From Dev

Tastypie return data from DELETE requests?

From Dev

tastypie return empty resource_uri in get

From Dev

How to return data in POST request (django/tastypie)

From Dev

Django tastypie, possible to return only metadata for a query

From Dev

How to return data in POST request (django/tastypie)

From Dev

Retrofit2 return null Unit in kotlin for 204 No Content response

From Dev

How to return file content using asynchronous callback response?

From Dev

Retrofit2 return null Unit in kotlin for 204 No Content response

Related Related

  1. 1

    Web API POST MultipartFormDataContent: Can response return multipartform content?

  2. 2

    Web API POST MultipartFormDataContent: Can response return multipartform content?

  3. 3

    Tastypie API return only one object for given parameters

  4. 4

    PHP bing api html content translation response displays as plain text

  5. 5

    PHP bing api html content translation response displays as plain text

  6. 6

    Wikipedia API doesn't return the same content that is in HTML

  7. 7

    Content negotiation to return HTML

  8. 8

    Tastypie api for multiple models

  9. 9

    Return content with IHttpActionResult for non-OK response

  10. 10

    Get the content HTML of an API

  11. 11

    Exclude a field from tastypie api

  12. 12

    Django Tastypie Postgres slow server response

  13. 13

    tastypie overid create_response() method

  14. 14

    Setting the charset of html response content to 1252

  15. 15

    How to access _content from a api response in python

  16. 16

    Return html response to calling function in nodejs

  17. 17

    Is it possible to return a response from a Web API constructor?

  18. 18

    Return response from API as inner JSON objects

  19. 19

    Web API AuthorizeAttribute does not return custom response

  20. 20

    Web API AuthorizeAttribute does not return custom response

  21. 21

    Return user_id as response of post api

  22. 22

    Tastypie return data from DELETE requests?

  23. 23

    tastypie return empty resource_uri in get

  24. 24

    How to return data in POST request (django/tastypie)

  25. 25

    Django tastypie, possible to return only metadata for a query

  26. 26

    How to return data in POST request (django/tastypie)

  27. 27

    Retrofit2 return null Unit in kotlin for 204 No Content response

  28. 28

    How to return file content using asynchronous callback response?

  29. 29

    Retrofit2 return null Unit in kotlin for 204 No Content response

HotTag

Archive