#๐Ÿ”’ keep getting CORS error

13 messages ยท Page 1 of 1 (latest)

knotty bobcatBOT
#

Hey @fair bolt!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
#

@fair bolt

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mystic grail
#

You don't need to do those add response headers

#

But move them into your CORS

#

Here's mine:
CORS(app, resources={r"/*": {"origins": ["http://localhost:9000", "*"]}})

fair bolt
#

ive added it

#
import json
from flask import Flask, jsonify, request
from flask_cors import CORS

app = Flask(__name__)

# CORS configuration
@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
    return response


def application(environ, start_response):
  if environ['REQUEST_METHOD'] == 'OPTIONS':
    start_response(
      '200 OK',
      [
        ('Content-Type', 'application/json'),
        ('Access-Control-Allow-Origin', '*'),
        ('Access-Control-Allow-Headers', 'Authorization, Content-Type'),
        ('Access-Control-Allow-Methods', 'POST'),
      ]
    )
    return ''

CORS(app, resources={r"/*": {"origins": ["http://localhost:3000", "*"]}})

@app.route('/computer-stats', methods=['GET'])
def get_computer_stats():
    cpu_usage = psutil.cpu_percent(interval=1)
    memory_usage = psutil.virtual_memory().percent
    disk_usage = psutil.disk_usage('/').percent
    network_usage = psutil.net_io_counters()
    
    temperatures = {}
    if hasattr(psutil, 'sensors_temperatures'):
        sensors_info = psutil.sensors_temperatures()
        for sensor_type, sensor_data in sensors_info.items():
            for entry in sensor_data:
                temperatures[entry.label] = entry.current

 
    stats = {
        'cpu_usage': cpu_usage,
        'memory_usage': memory_usage,
        'disk_usage': disk_usage,
        'network_usage': {
            'bytes_sent': network_usage.bytes_sent,
            'bytes_received': network_usage.bytes_recv
        },
        'temperatures': temperatures,
        'fan_speeds': fan_speeds,
        'voltages': voltages
    }

    return jsonify(stats)

if __name__ == '__main__':
    app.run(debug=True)```
knotty bobcatBOT
#

Hey @fair bolt!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
fair bolt
#

it works when i visit the browser but when i try to call fetch from nextjs it does 403

#

Reason: Multiple CORS header 'Access-Control-Allow-Origin' not allowed

knotty bobcatBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ keep getting CORS error