1

I'm trying to build a simple API with FastAPI that takes and receives data from the Shopify API. I'm currently trying to write an endpoint for a POST request made from Shopify.

The Shopify API includes a header that can be used to verify the request actually came from Shopify. Their documentation however uses Flask, whereas I'm trying to use FastAPI.

https://shopify.dev/apps/webhooks/configuration/https

I need to retrieve the request data as a bytestring and a header from the request. I was able to retrieve the header eventually (very new to all this). But so far am not sure how to get the bytestring.

This is what they use in the documentation

data = request.get_data() https://tedboy.github.io/flask/generated/generated/flask.Request.get_data.html

I haven't found an equivalent in FastAPI. Is there any? Or is there a way to take the data from the request and convert it to a bytestring to use in Shopify's verification example?

Any help is appreciated. Please let me know if I need to include more information.

Thanks for reading!

savdbroek
  • 11
  • 1
  • Related answers can also be found [here](https://stackoverflow.com/a/71650857/17865804) and [here](https://stackoverflow.com/a/70636163/17865804), in case you had to deal with arbitrary form-data or JSON, etc. – Chris Oct 13 '22 at 19:48

2 Answers2

0

i think you can use .json method from FastAPI

data_as_json = await request.json()
  • Thanks! I'm close to figuring it out. I use the request.json() you mentioned, use json.dumps() to dump it to a string and encode it with utf-8. It now gets parsed by the Shopify verify function, sadly it comes back as False. So I assume the data I'm parsing in is not same as what Shopify does on their end with request.get_data() from flask. – savdbroek Oct 13 '22 at 20:56
0

For shopify devs, use

data = await request.body()

This is the byte result.

yqleee
  • 1
  • 1