Shopify Webhook - Python

jsaya
Shopify Partner
25 0 0

Hi All, 

I have created a webhook and I am trying to get the response data from the POST request to my server. 

I am able to see the request but I am getting a 405 error which I cannot find in the Shopify response codes. 

How can I get the data from this webhook in order to delete the shop from out DB?

Any help would be awesome!

Thank you,

Jonah 

Replies 8 (8)

Josh
Shopify Staff
1134 84 233

Hey Jonah, 

I don't think a 405 response would be coming from us - here's a bit more info on them.

Do you have any other details on what you're trying to do? Are you trying to get the response from the webhook creation itself? Or are you trying to read the webhook data itself?

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

jsaya
Shopify Partner
25 0 0

Hey Josh, 

Appreciate the quick response.

All I am trying to do it create a webhook in order to detect when our ap is uninstalled and therefore use the data from the webhook to delete the shop in our database.

All I have so far is the following: 

#create webhookw for shop
webhook = shopify.Webhook()
webhook.topic = 'app/uninstalled'
webhook.address = '/webhook'
webhook.save()

I also have a route set to "/webhook" to recieve the response.

However this is where I am running into the 405 error.

So I guess a better questions might be what do I need to do after the step above in order to use the webhook to get some pieces of data(s anything like shop name, shared secret would work) that can be used to delete the shop from our database?

jsaya
Shopify Partner
25 0 0

EDIT:

 

I have fixed the 405 error and am now getting a 200 OK response. 

 

However when I go to pring the params of the POST I am getting an ImmutableDictionary that appears to be empty... How can I access the above data with this webhook response?

I am getting this response:

2018-01-30T01:27:21.924069+00:00 app[web.1]: --------------------

2018-01-30T01:27:21.924089+00:00 app[web.1]: ImmutableMultiDict([])

2018-01-30T01:27:21.924091+00:00 app[web.1]: ---------------------

2018-01-30T01:27:21.924826+00:00 app[web.1]: 10.168.69.189 - - [30/Jan/2018 01:27:21] "POST /webhook HTTP/1.1" 200 -

From this code:

params = request.args
    print('--------------------')
    print(params)
    print("---------------------")

 

Josh
Shopify Staff
1134 84 233

Hey again Jonah, 

Webhook POST requests will have the information within the request body. I'm not a Python developer so I won't be of much help with your code, but a Google search looked like request.args is looking for query parameters. Are you sure that the variable you're creating as 'params' has something in it? 

I'm not sure what Python has in terms of a debugger, but I'd recommend using one if possible to step through this and see if you're reading the request correctly. 

 

 

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

jsaya
Shopify Partner
25 0 0

Hey Josh,

Request.args is not the rcorrect thing to be usung here youre correct. 

Where can I see what this request body will look like?  

Josh
Shopify Staff
1134 84 233

It's definitely a trickier webhook to read since you can't just send yourself a test webhook for it and have to actually perform the action to fire it. 

I found some sample Python code that looks like it reads webhooks though. Any chance this helps get you going in the right direction? 
 

https://gist.github.com/gavinballard/8513270

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

jsaya
Shopify Partner
25 0 0

Hey Josh,

This looks great, thank you for finding that. 

It looks like you pass the request into the function as "f" in order for it to read the web hook. Does this sound right to you? 

shahrukhAhmed
Shopify Partner
30 0 3

Hey, were you able to configure it?