Cherrypy cors - Invalid response header

50 Views Asked by At

I've been messing around with webhooks, and while I've had plenty of success using something like curl to send post requests, when I try to use my regular browser, the cors policy has been giving me a hard time

__requires__ = [
    'cherrypy_cors',
]

import cherrypy
import cherrypy_cors
from discord_webhook import DiscordWebhook

webhookAddress = '...'

class MyResource:
    @cherrypy_cors.tools.preflight(
                    allowed_methods=["POST"],
                    allowed_origin=['*'],)
    @cherrypy.expose
    @cherrypy.tools.json_in()
    def webhook(self):
        if cherrypy.request.method == "OPTIONS":
            pass
        elif cherrypy.request.method == "POST":
            webhook = DiscordWebhook(url=webhookAddress, content=cherrypy.request.json['content'])
            response = webhook.execute()
            return response
        return {'method': 'non-POST'}

    @classmethod
    def run(cls):
        cherrypy_cors.install()
        config = {
            '/': {
                'cors.expose.on': True,
            },
        }
        cherrypy.quickstart(cls(), config=config)


__name__ == '__main__' and MyResource.run()

if I run through using curl, I have no problems, and the response comes through nice and clean

curl -d "@request.json" -X POST -H "Content-Type: application/json" http://127.0.0.1:8080/webhook

{"id":"...","type":0,"content":"hello world","channel_id":"...","author":{"id":"...","username":"Captain Hook","avatar":null,"discriminator":"0000","public_flags":0,"flags":0,"bot":true},"attachments":[],"embeds":[],"mentions":[],"mention_roles":[],"pinned":false,"mention_everyone":false,"tts":false,"timestamp":"2023-09-07T05:42:07.634000+00:00","edited_timestamp":null,"flags":0,"components":[],"webhook_id":"..."}

I thought I had the problems using preflight cooked, but, I'm still getting errors when I run through a browser log from browser

If it helps any, I'm including the HTML that I'm using to send the request

<!DOCTYPE html>
<html>

<head>
  <title>Server Status</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://unpkg.com/[email protected]"></script>
  <script src="https://unpkg.com/mustache@latest"></script>
  <script src="https://unpkg.com/htmx.org/dist/ext/client-side-templates.js"></script>
  <script src="https://unpkg.com/htmx.org/dist/ext/json-enc.js"></script>
  <script src="https://unpkg.com/htmx.org/dist/ext/debug.js"></script>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>

<body>
  <div class="container-fluid mt-3">
    <h1><small>Minecraft server status</small></h1>
  </div>


  <form hx-post="http://localhost:8080/webhook" hx-headers='{"Content-Type":"application/json"}' hx-ext="js-enc">
    <label>Content: <input type="text" name="content"></label>
    <input type="submit" value="Submit">
  </form>
  <div id="response"></div>


</body>

</html>

I was expected cherrypy_cors to take care of the preflight request and return a properly formatted response, instead, using chrome, I receive an error

Access to XMLHttpRequest at 'http://localhost:8080/webhook' from origin 'http://127.0.0.1:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

0

There are 0 best solutions below