Skip to main content
Question

Why would Urllib.request.urlopen return b'' when running a webhook?


I am trying to run webhooks on FME Server. The webhooks run successfully and I get status code 200 from the server but when I check the length of the data in the response it is 0, in this case 0. Is there a header I am missing?

from urllib.request import urlopen
from shutil import copyfileobj
def runwebhook(runurl):
    response = urlopen(runurl)
    if response.getcode()==200:
        print("Success")
    return response
def savefilestream(filepath,response):
    if exists(filepath): remove(filepath)
    if isinstance(response.read(),bytes)==True:
        with open(filepath,'wb'as f:
            data = len(response.read())
            print(data)
            copyfileobj(response, f)
            f.close()
            print("File Saved Successfully")
    else:
        with open(filepath,'w'as f:
            f.write(str(response.read()))
            f.close()
            print("File Saved as text")
def runwebhookwithheaders(runurl,hooktoken):
    runresponse = None
    headers = {
    'Authorization':f"fmetoken token={hooktoken}",
    'Accept''application/octet-stream',
    'Content-Type'"application/octet-stream",
    'accept-encoding'"gzip, deflate",
    'Content-Transfer-Encoding''binary',
    'Connection'"keep-alive"
    }
    req = Request(self.runurl,headers=headers)
    try:
        runresponse = urlopen(req)
    
    except HTTPError as e:
        print(e.code)
        print(e.code)
    except URLError as e:
        print(e.reason)
        print(e.reason)
    return runresponse
#example usage for a simple webhook
filepath= r":\filepath\\"
runurl = "%%Webhookurl%%"
resp = runwebhook(runurl)
savefilestream(filepath,resp)
# example with token
filepath= r":\filepath\\"
runurl = "%%Webhookurl%%"
hooktoken = "token"
resp = runwebhookewithheaders(runurl,token)
savefilestream(filepath,resp)

 

 

 

2 replies

Hi @garydlester​ , thank you for your question. Would you be able to provide a bit of background on your workflow? What webhook are you trying to connect to? Is FME Server the sending or receiving side of the connection?


I have it sorted now, I think in the last server update there was a change made and webhooks run different, we figured it out. Thanks.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings