7

Say I have a Resource that does not do anything but returns the url to the console

from app import api

class StaticFiles(Resource):
    def get(self):
        return api.url_for(self) # this only provides the resource url 

if the request was http://localhost:5000/static code above returns /static I am looking for http://localhost:5000/static

Usually I use requests but there is no request in resources. I am looking for pretty much the equivalent of request.base_url

Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
Evren Bingøl
  • 1,306
  • 1
  • 20
  • 32

1 Answers1

7

request is a global object which you can access by importing request from flask:

from flask import request
from app import api

class StaticFiles(Resource):
def get(self):
    return(request.base_url)

There are lots of nice alternative formats of the url here, see this answer for more details.

Rob Bricheno
  • 4,467
  • 15
  • 29
  • ohhh what, Thanks I though request was to be used with only route decorator. What an Idiot I am, I guess it is a thread local object. You wont believe the things I tried. – Evren Bingøl Nov 20 '18 at 17:13
  • @EvrenBingøl No problem, I don't think this is obvious and I also don't think this is a duplicate question as none of the other answers actually tell you how to get a hold of the `request` in the first place! – Rob Bricheno Nov 20 '18 at 18:27