Search code examples
androidwebflask-restful

Rendering different response to web and android app


I saw this answer (2nd one), where it is advised to add a field (eg. device = "web") in request to decide what kind of response to be returned to a web or an android app, from a REST API.

Suggested way:

@app.route('/test', methods=['GET'])
def test():
    device = request.args.get('device')

    if device is "web":
        return render_template('test.html', data='Hello Word')
    else:
        # Return data to Android Application
        return json.dumps({'data':'Hello World'})

I found it pretty useful, however, author himself/herself says that it's a crude way to do it, and that there are better ways as well.

Please suggest me a better way, thanks.


Solution

  • I'm not familiar with Flask, but from a general, language/platform-agnostic perspective, IMO the best way is probably to build your REST API in a front-end agnostic way. Then your web app, mobile app, and whatever else in the future all use it the same way.