0

I'm attempting to pull values out of a post request from slack inside of fast api. According to the slack API I should be receiving a json payload. The body I capture appears to be encoded and I'm unsure how to get it into json form.

What I have

async def get_body(request: Request):
    return await request.body()


@app.post("/slack", status_code=200)
async def recieveSlackInteraction(body = Depends(get_body)):
    decode = body.decode("utf-8")
    decode2 = urllib.parse.unquote(decode)
    with open("data_file.json", "w") as write_file:
        json.dump(decode2.strip('"'), write_file)

    
    return 

The above gives me this. How do I turn this into a json object I can access normally?

"payload={\"type\":\"block_actions\",\"user\":{\"id\":\"UKLSJDHFUMR\",\"username\":\"user\",\"name\":\"user\",\"team_id\":\"TDFSDSSDJ\"},\"api_app_id\":\"A0SDFSDFDSFL2U\",\"token\":\"SDFDSFSDFSDFYgk\",\"container\":{\"type\":\"message\",\"message_ts\":\"SDFSDFSDFDSF19\",\"channel_id\":\"GSDFSDFSDFSDFPB\",\"is_ephemeral\":false},\"trigger_id\":\"SDFSDFSDFDSFSDFSDFSDF\",\"team\":{\"id\":\"SDFSDFSDFSDFSDF\",\"domain\":\"SDFSDFSDF\"},\"enterprise\":null,\"is_enterprise_install\":false,\"channel\":{\"id\":\"SDFSDFSDF\",\"name\":\"privategroup\"},\"message\":{\"type\":\"message\",\"subtype\":\"bot_message\",\"text\":\"This+content+can't+be+displayed.\",\"ts\":\"SDFSDFSDF\",\"bot_id\":\"SDFSDFSDFDSF\",\"blocks\":[{\"type\":\"section\",\"block_id\":\"bOsst\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"<http:\\/\\/SDFSDF|SDFSDFSDF>\",\"verbatim\":false}},{\"type\":\"actions\",\"block_id\":\"sRgge\",\"elements\":[{\"type\":\"button\",\"action_id\":\"actionId-0\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click+Me\",\"emoji\":true},\"style\":\"danger\",\"value\":\"REBOOT\"}]}]},\"state\":{\"values\":{}},\"response_url\":\"https:\\/\\/hooks.slack.com\\/actions\\/SDFSDF\\/SDFSDFSDF\\/SDFSDFSDFDSFDF\",\"actions\":[{\"action_id\":\"actionId-0\",\"block_id\":\"sRgge\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click+Me\",\"emoji\":true},\"value\":\"REBOOT\",\"style\":\"danger\",\"type\":\"button\",\"action_ts\":\"1ASDASDASD"}]}"
3
  • Why are you stripping out all of the quotes? They need to be there... Commented Aug 18, 2022 at 13:57
  • 1
    Why not just use request.json()? Commented Aug 18, 2022 at 14:03
  • @MattDMo Not all of them , I just need to lose the outer most quotes so I can access the object. Commented Aug 18, 2022 at 14:09

1 Answer 1

1

Regex would be a good way (which I will code up later). But basically you need to fix up the response into a valid json:

import json

jsonStr = '''"payload={\"type\":\"block_actions\",\"user\":{\"id\":\"UKLSJDHFUMR\",\"username\":\"user\",\"name\":\"user\",\"team_id\":\"TDFSDSSDJ\"},\"api_app_id\":\"A0SDFSDFDSFL2U\",\"token\":\"SDFDSFSDFSDFYgk\",\"container\":{\"type\":\"message\",\"message_ts\":\"SDFSDFSDFDSF19\",\"channel_id\":\"GSDFSDFSDFSDFPB\",\"is_ephemeral\":false},\"trigger_id\":\"SDFSDFSDFDSFSDFSDFSDF\",\"team\":{\"id\":\"SDFSDFSDFSDFSDF\",\"domain\":\"SDFSDFSDF\"},\"enterprise\":null,\"is_enterprise_install\":false,\"channel\":{\"id\":\"SDFSDFSDF\",\"name\":\"privategroup\"},\"message\":{\"type\":\"message\",\"subtype\":\"bot_message\",\"text\":\"This+content+can't+be+displayed.\",\"ts\":\"SDFSDFSDF\",\"bot_id\":\"SDFSDFSDFDSF\",\"blocks\":[{\"type\":\"section\",\"block_id\":\"bOsst\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"<http:\\/\\/SDFSDF|SDFSDFSDF>\",\"verbatim\":false}},{\"type\":\"actions\",\"block_id\":\"sRgge\",\"elements\":[{\"type\":\"button\",\"action_id\":\"actionId-0\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click+Me\",\"emoji\":true},\"style\":\"danger\",\"value\":\"REBOOT\"}]}]},\"state\":{\"values\":{}},\"response_url\":\"https:\\/\\/hooks.slack.com\\/actions\\/SDFSDF\\/SDFSDFSDF\\/SDFSDFSDFDSFDF\",\"actions\":[{\"action_id\":\"actionId-0\",\"block_id\":\"sRgge\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click+Me\",\"emoji\":true},\"value\":\"REBOOT\",\"style\":\"danger\",\"type\":\"button\",\"action_ts\":\"1ASDASDASD"}]}"'''
jsonStr = jsonStr.split('"payload=')[-1].replace('\"','"').rsplit('"', 1)[0]

jsonData = json.loads(jsonStr)

Using Regex:

import re
import json

jsonStr = '''"payload={\"type\":\"block_actions\",\"user\":{\"id\":\"UKLSJDHFUMR\",\"username\":\"user\",\"name\":\"user\",\"team_id\":\"TDFSDSSDJ\"},\"api_app_id\":\"A0SDFSDFDSFL2U\",\"token\":\"SDFDSFSDFSDFYgk\",\"container\":{\"type\":\"message\",\"message_ts\":\"SDFSDFSDFDSF19\",\"channel_id\":\"GSDFSDFSDFSDFPB\",\"is_ephemeral\":false},\"trigger_id\":\"SDFSDFSDFDSFSDFSDFSDF\",\"team\":{\"id\":\"SDFSDFSDFSDFSDF\",\"domain\":\"SDFSDFSDF\"},\"enterprise\":null,\"is_enterprise_install\":false,\"channel\":{\"id\":\"SDFSDFSDF\",\"name\":\"privategroup\"},\"message\":{\"type\":\"message\",\"subtype\":\"bot_message\",\"text\":\"This+content+can't+be+displayed.\",\"ts\":\"SDFSDFSDF\",\"bot_id\":\"SDFSDFSDFDSF\",\"blocks\":[{\"type\":\"section\",\"block_id\":\"bOsst\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"<http:\\/\\/SDFSDF|SDFSDFSDF>\",\"verbatim\":false}},{\"type\":\"actions\",\"block_id\":\"sRgge\",\"elements\":[{\"type\":\"button\",\"action_id\":\"actionId-0\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click+Me\",\"emoji\":true},\"style\":\"danger\",\"value\":\"REBOOT\"}]}]},\"state\":{\"values\":{}},\"response_url\":\"https:\\/\\/hooks.slack.com\\/actions\\/SDFSDF\\/SDFSDFSDF\\/SDFSDFSDFDSFDF\",\"actions\":[{\"action_id\":\"actionId-0\",\"block_id\":\"sRgge\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click+Me\",\"emoji\":true},\"value\":\"REBOOT\",\"style\":\"danger\",\"type\":\"button\",\"action_ts\":\"1ASDASDASD"}]}"'''


jsonStr = re.match('\"payload=({.*})', jsonStr).group(1)
jsonData = json.loads(jsonStr)
Sign up to request clarification or add additional context in comments.

1 Comment

That worked beautifully! I've been wrestling this for hours and posted here after getting it down from bytes. Thank you so much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.