Forums

301 Moved Permanently when using the always on restart endpoing via POST

I'm trying to use requests.post to trigger the restart endpoint for an always on task. I'm getting "Unexpected status code: 301" Moved Permanently response (using allow_redirects=False for debugging).

Here's my code. I couldn't find any examples of this, and the API documentation is pretty limited.

import requests
username = '<username>'
token = '<token>'
host = 'www.pythonanywhere.com'

response = requests.post(
    'https://{host}/api/v0/user/{username}/always_on/46244/restart'.format(host=host, username=username),
    headers={'Authorization': 'Token {token}'.format(token=token)},
    allow_redirects=False)

if response.status_code == 200:
    print('CPU quota info:')
    print(response.content)
else:
    print('Got unexpected status code {}: {!r}'.format(response.status_code, response.content))

Try with 'https://{host}/api/v0/user/{username}/always_on/46244/restart/ with / at the end.

Thanks! Now I get a 500 error with an html file returned that says, among other things, "Sorry, there was an error connecting to the server." What am I doing wrong?

Also: I got the same response whether I allowed redirects or not.

Make sure that the task id is indeed 46244.

It was a typo in the task ID. Thanks!

Thanks for confirming that!