I have a localhost jupyter notebook python file which I’m trying to send data to my api/python/index.ts file and fetch said data from my index.vue page using useFetch('/api/python/') but am receiving a get request error
Using nuxt3
Error message:
GET http://localhost:3000/api/python 405 (HTTP method is not allowed.)
code:
localhost python script
url = 'http://localhost:3000/api/python'
headers = {'Content-type': 'application/json'}
response = requests.post(url, json=top_3_json, headers=headers)
print(response.json())
{'status': 200, 'body': '{"surfboards": \[{"id": 22, "value": 0.04}, {"id": 45, "value": 0.04}, {"id": 46, "value": 0.03}\]}', 'headers': {'Content-Type': 'application/json'}}
‘/server/api/python/index.ts’
The request handler code
code source -https://nuxt.com/docs/guide/directory-structure/server#catch-all-route (#handling requests with body)
export default defineEventHandler(async (event) => {
const body = await readBody(event)
console.log(data)
return { body }
})
console log output:
{"surfboards": [{"id": 22, "value": 0.04}, {"id": 45, "value": 0.04}, {"id": 46, "value": 0.03}]}
‘/pages/index.vue’
export default {
setup() {
const { data } = useFetch('/api/python/')
return { data }
}
In the browser console I receive this error message:
GET http://localhost:3000/api/python 405 (HTTP method is not allowed.)
The server-side file is returning the data but it cannot be accessed from the client side
Any help would be much appreciated,
Jack
>Solution :
In the Python script, you’re making a POST request.
The error message from your other code says you’re making a GET request, which the endpoint doesn’t handle (hence the 405 status code).
If the Python code is successful, then you should make a POST request in your other client, too.