Forums

Requests.post to an external webhook not working

I am trying to send a process-completion message to an external webhook using the following code:

def send_results_to_callback(order_id, url, result):
    try:
        payload = {
            'process_id': IMAGEGEN_ID,
            'order_id': order_id,
            'status': result['ok'],
            'reponse': result['response']
        }

        headers = {
            'Content-Type': 'application/json',
            'Authorization': f"Basic {IMAGEGEN_USER_PW_CREDS}",
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36',
            'Cache-Control': 'no-cache',
            'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate, br'
        }
        resp = requests.post(url, json=payload, headers=headers, timeout=60)
        resp.raise_for_status()

I am getting a timeout exception when I run this on pythonanywhere. This code runs on my laptop and the same post also works in Postman.

I have tried various header parameters including leaving the 'User-Agent' out.

I have also determined that the webhook server is not receiving the post.

Any suggestions?

Thanks in advance,

John Underwood

[edit by admin: formatting]

If the url is the same on Postman and locally as it is when you're running on PythonAnywhere, then it sounds like the server you're POSTing to is blocking the incoming requests -- you'd need to check with the admins of that server to find out why they might be doing that, but some people do block incoming connections from cloud environments like PythonAnywhere.

Thanks giles. I'll check that.