Code Monkey home page Code Monkey logo

Comments (5)

dougblack avatar dougblack commented on May 10, 2024 1

If the return object of a resource method (get() or post()) is not a Response object, Flask-RESTful calls make_response() on that object. make_response() basically only responds with a mediatype from [request.accept_mimetypes, api.default_mediatype] which in your case wouldn't return text/html unless the request had an accept header that included text/html or you had assigned text/html to api.default_mediatype.

So, the fix is to make sure you return a Response object from your resource method so Flask-RESTful doesn't try to create its own.

It sounds like you'd want to do this:

def output_html(data, code, headers=None):
    resp = Response(format_data(data), mimetype='text/html', headers=headers)
    resp.status_code = code
    return resp

class Foo(restful.Resource):
    def get(self):
        return output_html(some_python_object, 200)

from flask-restful.

saml avatar saml commented on May 10, 2024

I could do the following:

def output_html(data, code, headers=None):
    resp = Response(format_data(data), mimetype='text/html', headers=headers)
    resp.status_code = code
    return resp

class Foo(restful.Resource):
    def get(self):
        return some_python_object
    representations = {'text/html': output_html, 'application/json': output_html}

Problem is that flask-restful doesn't obey mimetype parameter I specified in Response. It still responds with Content-Type: application/json, not Content-Type: text/html.

from flask-restful.

dougblack avatar dougblack commented on May 10, 2024

The response here was wrong. Hold on while I fix it.

from flask-restful.

saml avatar saml commented on May 10, 2024

Ah, that works beautifully. I was gonna override dispatch_request. But returning Response is better.

class Foo(restful.Resource):
    def get(self):
        resp = Response('hello', mimetype='text/html')
        return resp

from flask-restful.

wasauce avatar wasauce commented on May 10, 2024

@dougblack @saml this was really helpful -- thanks for posting this so many years ago :)

from flask-restful.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.