Search code examples
pythonrestherokuaiohttp

Python API Unable to GET request using aiohttp


I'm trying to make my own api using aiohttp. It works perfectly fine on localhost:8080, Is there a way to connect it into heroku site , I tried to load with https://dumboapi.herokuapp.com/getmeme/ but it doesn't work :/ This is my code:

subreddit = ['memes', 'dankmemes', 'funny']

HEADERS = {
    'User-Agent' : "Dumbo"
}
async def getmeme():
    async with request("GET", f"https://www.reddit.com/r/{choice(subreddit)}/new.json?limit=100", headers=HEADERS) as resp:
        data = await resp.json()
        link_data = data['data']['children'][2]['data']['url_overridden_by_dest']
        title_data = data['data']['children'][2]['data']['title']
        score_data = data['data']['children'][2]['data']['score']
        submission = data['data']['children'][2]['data']['subreddit_name_prefixed']
        meme_data = {'image': f'{link_data}', 'title': f'{title_data}', 'score': f'{score_data}', 'subreddit': f'{submission}'}
        return meme_data

@routes.get('/getmeme')
async def handle(request):
    response = await getmeme()
    return web.Response(text=json.dumps(response))

async def initialize():
    app = web.Application()
    app.add_routes(routes)
    return app

web.run_app(initialize())

Error from Heroku:

2020-10-14T05:30:55.726041+00:00 heroku[worker.1]: Restarting
2020-10-14T05:30:55.739241+00:00 heroku[worker.1]: State changed from up to starting
2020-10-14T05:30:56.951514+00:00 heroku[worker.1]: Stopping all processes with SIGTERM
2020-10-14T05:30:57.134832+00:00 heroku[worker.1]: Process exited with status 0
2020-10-14T05:30:58.635865+00:00 heroku[worker.1]: Starting process with command `python main.py`
2020-10-14T05:30:59.240689+00:00 heroku[worker.1]: State changed from starting to up
2020-10-14T05:31:00.887027+00:00 app[worker.1]: ======== Running on http://0.0.0.0:8080 ========
2020-10-14T05:31:00.887045+00:00 app[worker.1]: (Press CTRL+C to quit)
2020-10-14T05:31:08.758433+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/getmeme/" host=dumboapi.herokuapp.com request_id=39a1af4e-c49b-45a5-b925-927ae0236996 fwd="IP" dyno= connect= service= status=503 bytes= protocol=https
2020-10-14T05:31:09.853957+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=dumboapi.herokuapp.com request_id=98f603ac-700f-495d-812a-23608c42fccd fwd="IP" dyno= connect= service= status=503 bytes= protocol=https

Solution

  • Here it says that Only web dynos receive HTTP traffic from the routers.

    From what I see, I suppose you are not using a web process type.

    2020-10-14T05:31:08.758433+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/getmeme/" host=dumboapi.herokuapp.com request_id=39a1af4e-c49b-45a5-b925-927ae0236996 fwd="IP" dyno= connect= service= status=503 bytes= protocol=https
    2020-10-14T05:31:09.853957+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=dumboapi.herokuapp.com request_id=98f603ac-700f-495d-812a-23608c42fccd fwd="IP" dyno= connect= service= status=503 bytes= protocol=https
    

    Change the process type to web in the Procfile also read the port number from the environment (PORT variable) that heroku provides.