当前位置:网站首页>Fastapi 5 - common requests and use of postman and curl (parameters, x-www-form-urlencoded, raw)
Fastapi 5 - common requests and use of postman and curl (parameters, x-www-form-urlencoded, raw)
2022-06-11 22:20:00 【Yizhi code】
List of articles
This article is suitable for web Request unfamiliar back-end novices .
Some concepts are not very clear , But the first thing to do .
more fastapi Use visible my blog :https://so.csdn.net/so/search?q=fastapi&t=blog&u=lovechris00
Or official documents :https://fastapi.tiangolo.com
Be careful :postman On-line web test , May not be able to connect to your local interface , Because it is not on a LAN .
api After starting , You can access the corresponding docs, Such as http://127.0.0.1:8124/docs#/
get
from fastapi import FastAPI, Form, Request
from pydantic import BaseModel
@app.get("/item/{text}")
async def read_item(text: str):
print("\n== get text : ", text)
if len(text) == 0:
return 'no text'
return {
"type": text}
request
import requests
import json
IP = '127.0.0.1'
PORT = '8120'
url = f'http://{
IP}:{
PORT}/item/{
text}'
ret = requests.get(url=url, timeout=10)
print(ret, ret.text)
curl -X 'GET' \
'http://127.0.0.1:8116/item/beautiful' \
-H 'accept: application/json'

parameters
@app.post("/item5/")
async def func5(text: str = ''):
return {
"type": text}
text = 'beautiful'
url = f'http://{
IP}:{
PORT}/item5/?text={
text}'
ret = requests.post(url=url)
print(ret, ret.text)
curl -X 'POST' \
'http://127.0.0.1:8116/item5/?text=beautiful' \
-H 'accept: application/json' \
-d ''

body - form-data
class TextItem(BaseModel):
text: str
type: str = None
score: float = None
# form data -
@app.post("/item1/")
async def func1(item: TextItem):
print("\n-- get form-data : ", item.dict())
text = item.text
# print('-- text : ', text)
return {
"type": text}
request
url = f'http://{
IP}:{
PORT}/item1/'
dict = {
'text': text}
json_str = json.dumps(dict)
ret = requests.post(url=url, data=json_str)
print(ret, ret.text)
– get form-data : {‘text’: ‘beautiful’, ‘type’: None, ‘score’: None}
INFO: 127.0.0.1:54549 - “POST /item1/ HTTP/1.1” 200 OK
curl -X 'POST' \
'http://127.0.0.1:8120/item1/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{ "text": "beautiful", "type": "string", "score": 0 }'
x-www-form-urlencoded
@app.post("/item2/")
async def func2(text: str = Form(...)):
print("\n-- get urlencoded text : ", text )
return {
"type": text}
request
url = f'http://{
IP}:{
PORT}/item2/'
dict = {
'text': text}
# data = json.dumps(dict)
payload=parse.urlencode(dict)
print('-- payload : ', payload)
headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" }
ret = requests.post(url, data=payload, headers=headers)
print(ret, ret.text)
curl -X 'POST' \
'http://127.0.0.1:8120/item2/' \
-H 'accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'text=beautiful'
postman in , Choose body-x-www-form-urlencoded You can not set it header
form-data stay postman It seems that this interface is the only way to get through

raw
import json
@app.post("/item3/")
async def func3(request: Request):
body = await request.body()
print('\n-- get raw body : ', body)
json_str = str(body, encoding='utf-8')
dict = json.loads(json_str)
# print('-- body dict : ', dict)
text = dict['text']
# print("-- text : ", text )
return {
"type": text}
request
json_str = json.dumps(dict)
ret = requests.post(f'http://{
IP}:{
PORT}/item3/', data=json_str)
print(ret, ret.text)
curl -X 'POST' \
'http://127.0.0.1:8120/item3/' \
-H 'accept: application/json' \
-d '{"text":"beautiful"}'

2022-06-10( 5、 ... and )
Programming 7 Anniversary of the ~~
边栏推荐
- Dynamic memory management (1)
- [Chongqing Guangdong education] college physics of Xiangtan University: mechanical and thermal reference materials
- Analysis of the implementation principle of an open source markdown to rich text editor
- Go encoding package
- 习题10-1 判断满足条件的三位数 (15 分)
- Simple example of logistic regression for machine learning
- 每日一题-1317. 将整数转换为两个无零整数的和
- How to adjust the font blur of win10
- 判断链表是否为回文结构
- Go IO module
猜你喜欢

The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace

Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.3

超标量处理器设计 姚永斌 第2章 Cache --2.3 小节摘录

Regular execution of shell scripts in crontab

How to adjust the font blur of win10

二叉树的基本操作与题型总结

Analysis of the implementation principle of an open source markdown to rich text editor

Uncover the secret of the popular app. Why is it so black

打印机无法打印测试页是什么原因

Nmap performs analysis of all network segment IP survivals in host detection
随机推荐
判断链表是否为回文结构
Matplotlib和tkinter学习笔记(一)
How to view computer graphics card information in win11
详解异步任务:函数计算的任务触发去重
One question of the day - delete duplicates of the ordered array
[Chongqing Guangdong education] college physics of Xiangtan University: mechanical and thermal reference materials
移动端——swipe特效之图片时间轴
习题8-8 判断回文字符串 (20 分)
[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?
Basic operation of graph (C language)
What is deadlock? (explain the deadlock to everyone and know what it is, why it is used and how to use it)
C language implements eight sorts (3)
R language book learning 03 "in simple terms R language data analysis" - Chapter 8 logistic regression model Chapter 9 clustering model
Custom implementation offsetof
Collection of articles and literatures related to R language (continuously updated)
Addition without addition, subtraction, multiplication, Division
[niuke.com] ky41 put apples
STM32 development note 113:ads1258 drive design - reading temperature value
Maze problem in C language
重温c语言一