You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
6.1 KiB

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FastAPI 请求测试</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
.form-group button {
padding: 10px 15px;
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
}
.form-group button:hover {
background-color: #0056b3;
}
.result {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
background-color: #f9f9f9;
}
.result pre {
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
</head>
<body>
<h1>FastAPI 请求测试</h1>
<div class="form-group">
<h2>GET 请求</h2>
<label for="get-username">用户名:</label>
<input type="text" id="get-username" name="username" value="测试用户名1" required>
<label for="get-password">密码:</label>
<input type="password" id="get-password" name="password" value="123123" required>
<button onclick="sendGetRequest()">发送 GET 请求</button>
</div>
<div class="form-group">
<h2>POST 请求(表单数据)</h2>
<label for="post-username-form">用户名:</label>
<input type="text" id="post-username-form" name="username" value="测试用户名2" required>
<label for="post-password-form">密码:</label>
<input type="password" id="post-password-form" name="password" value="123123" required>
<button onclick="sendPostFormRequest()">发送 POST 请求(表单数据)</button>
</div>
<div class="form-group">
<h2>POST 请求JSON 数据)</h2>
<label for="post-username-json">用户名:</label>
<input type="text" id="post-username-json" name="username" value="测试用户名3" required>
<label for="post-password-json">密码:</label>
<input type="password" id="post-password-json" name="password" value="123123" required>
<button onclick="sendPostJsonRequest()">发送 POST 请求JSON 数据)</button>
</div>
<div class="result" id="result"></div>
<script>
async function sendGetRequest() {
const username = document.getElementById('get-username').value;
const password = document.getElementById('get-password').value;
const url = `http://127.0.0.1:8888/api/test/parse?username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`;
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP error! Status: ${response.status}, Message: ${errorText}`);
}
const result = await response.json();
document.getElementById('result').innerHTML = `<pre>${JSON.stringify(result, null, 2)}</pre>`;
} catch (error) {
document.getElementById('result').innerHTML = `<pre>${error}</pre>`;
}
}
async function sendPostFormRequest() {
const username = document.getElementById('post-username-form').value;
const password = document.getElementById('post-password-form').value;
const url = `http://127.0.0.1:8888/api/test/parse`;
const formData = new FormData();
formData.append('username', username);
formData.append('password', password);
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json'
},
body: formData
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP error! Status: ${response.status}, Message: ${errorText}`);
}
const result = await response.json();
document.getElementById('result').innerHTML = `<pre>${JSON.stringify(result, null, 2)}</pre>`;
} catch (error) {
document.getElementById('result').innerHTML = `<pre>${error}</pre>`;
}
}
async function sendPostJsonRequest() {
const username = document.getElementById('post-username-json').value;
const password = document.getElementById('post-password-json').value;
const url = `http://127.0.0.1:8888/api/test/parse_json`;
const jsonData = {
username: username,
password: password
};
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP error! Status: ${response.status}, Message: ${errorText}`);
}
const result = await response.json();
document.getElementById('result').innerHTML = `<pre>${JSON.stringify(result, null, 2)}</pre>`;
} catch (error) {
document.getElementById('result').innerHTML = `<pre>${error}</pre>`;
}
}
</script>
</body>
</html>