简体   繁体   中英

Error TypeError: string indices must be integers json jupyter kernel gateway

So i have a small api in my ipynb with the following code

# POST /convert
req = json.loads(REQUEST)
args = req['body']
print(args['angle'])

angle = args['angle']
converted = math.radians(angle)
print(json.dumps({'convertedAngle': converted}))

and it works fine in postman however in the frontend website it gives me the following 2 errors

CORS-header 'Access-Control-Allow-Origin' is missing this one is logical as my other error makes it so that i dont return anything

Error TypeError: string indices must be integers this one is highly confusing as i am not even sure where this one comes from

is there a way to fix this problem so i can do post requests from my frontend website to my ipnb?

things i know that work:

converting it to a GET function allows it to work but is quite unsecure

postman with the following curl request:

curl --location --request POST 'http://127.0.0.1:8889/convert' \
--header 'Content-Type: application/json' \
--data-raw '{
    "angle": 180
}'

k so i figured it out the problem was the pre flight options request from CORS it refused to send the correct cors headers back because you need to set that up in config or with the following long command in your anaconda prompt jupyter kernelgateway --KernelGatewayApp.api='kernel_gateway.notebook_http' --KernelGatewayApp.seed_uri='api.ipynb' --KernelGatewayApp.allow_headers="*" --KernelGatewayApp.allow_origin='*'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM