Skip to content

Instantly share code, notes, and snippets.

@mgautam98
Last active July 9, 2021 18:08
Show Gist options
  • Save mgautam98/4a603a2c882fa221df6e97a632b17c4d to your computer and use it in GitHub Desktop.
Save mgautam98/4a603a2c882fa221df6e97a632b17c4d to your computer and use it in GitHub Desktop.
Reproduce fetch error
<!DOCTYPE html>
<html lang="en">
<head>
<!-- File loc: static/index.html -->
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<code id="code-block"></code>
</body>
<script>
const request = () => {
fetch("http://127.0.0.1:8000/task", {
'method': "POST",
'mode':'no-cors',
'headers': {
"Content-Type": "application/json"
},
body: JSON.stringify({"id":"string"})
})
.then(response => response.json())
.then(data => {
code = document.getElementById("code-block");
code.textContent = JSON.stringify(data, null, 4)
})
.catch(error => console.log(error))
}
request()
</script>
</html>
# main.py
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
app = FastAPI()
class Task(BaseModel):
id: str
@app.post("/task")
def create_task(data: Task):
return {"task_id": "id"}
# add index.html in /static
app.mount("/", StaticFiles(directory="static", html=True), name="static")
[tool.poetry]
name = "database"
version = "0.1.0"
description = ""
authors = ["Gautam Mishra <mishragautam96@gmail.com>"]
[tool.poetry.dependencies]
python = "^3.9"
fastapi = "^0.66.0"
uvicorn = "^0.14.0"
aiofiles = "^0.7.0"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment