当前位置: 首页 > news >正文

网站方案策划书18000字免费发布产品的平台

网站方案策划书18000字,免费发布产品的平台,网站回滚,网站建设可以一次性进损益吗前言 某些情况下,需要向客户端返回错误提示。 这里所谓的客户端包括前端浏览器、其他应用程序、物联网设备等。 需要向客户端返回错误提示的场景主要如下: 客户端没有执行操作的权限客户端没有访问资源的权限客户端要访问的项目不存在等等 … 遇到这些…

前言

某些情况下,需要向客户端返回错误提示。
这里所谓的客户端包括前端浏览器、其他应用程序、物联网设备等。
需要向客户端返回错误提示的场景主要如下:

  • 客户端没有执行操作的权限
  • 客户端没有访问资源的权限
  • 客户端要访问的项目不存在
  • 等等 …

遇到这些情况时,通常要返回 4XX(400 至 499)HTTP 状态码
4XX 状态码与表示请求成功的 2XX(200 至 299) HTTP 状态码类似。
只不过,4XX 状态码表示客户端发生的错误。

使用 HTTPException

向客户端返回 HTTP 错误响应,可以使用 HTTPException

from fastapi import FastAPI, HTTPExceptionapp = FastAPI()items = {"foo": "The Foo Wrestlers"}@app.get("/items/{item_id}")
async def read_item(item_id: str):if item_id not in items:raise HTTPException(status_code=404, detail="Item not found")return {"item": items[item_id]}

触发 HTTPException

HTTPException 是额外包含了和 API 有关数据的常规 Python 异常。
因为是 Python 异常,所以不能 return,只能 raise

如在调用_路径操作函数_里的工具函数时,触发了 HTTPException,FastAPI 就不再继续执行_路径操作函数_中的后续代码,而是立即终止请求,并把 HTTPException 的 HTTP 错误发送至客户端。

在介绍依赖项与安全的章节中,您可以了解更多用 raise 异常代替 return 值的优势。

本例中,客户端用 ID 请求的 item 不存在时,触发状态码为 404 的异常:

    raise HTTPException(status_code=404, detail="Item not found")

响应结果

请求为 http://example.com/items/fooitem_id 为 「foo」)时,客户端会接收到 HTTP 状态码 - 200 及如下 JSON 响应结果:

{"item": "The Foo Wrestlers"
}

但如果客户端请求 http://example.com/items/baritem_id 「bar」 不存在时),则会接收到 HTTP 状态码 - 404(「未找到」错误)及如下 JSON 响应结果:

{"detail": "Item not found"
}

触发 HTTPException 时,可以用参数 detail 传递任何能转换为 JSON 的值,不仅限于 str
还支持传递 dictlist 等数据结构。
FastAPI 能自动处理这些数据,并将之转换为 JSON。

添加自定义响应头

有些场景下要为 HTTP 错误添加自定义响应头。例如,出于某些方面的安全需要。

一般情况下可能不会需要在代码中直接使用响应头。

但对于某些高级应用场景,还是需要添加自定义响应头:

from fastapi import FastAPI, HTTPExceptionapp = FastAPI()items = {"foo": "The Foo Wrestlers"}@app.get("/items-header/{item_id}")
async def read_item_header(item_id: str):if item_id not in items:raise HTTPException(status_code=404,detail="Item not found",headers={"X-Error": "There goes my error"},)return {"item": items[item_id]}

响应结果

HTTP/1.1 404 Not Found
date: Sun, 24 Sep 2023 01:31:18 GMT
server: uvicorn
x-error: There goes my error
content-length: 27
content-type: application/json{"detail":"Item not found"}

文章转载自:
http://dinncodecagynous.wbqt.cn
http://dinncosector.wbqt.cn
http://dinncoorthopteran.wbqt.cn
http://dinncosourdine.wbqt.cn
http://dinncorhapsody.wbqt.cn
http://dinncoleukocytic.wbqt.cn
http://dinncosoftpanel.wbqt.cn
http://dinncopieplant.wbqt.cn
http://dinncoby.wbqt.cn
http://dinncocither.wbqt.cn
http://dinncodecastylar.wbqt.cn
http://dinncosunnite.wbqt.cn
http://dinncoexistentialism.wbqt.cn
http://dinncobrahmanic.wbqt.cn
http://dinncoedinburghshire.wbqt.cn
http://dinncocanape.wbqt.cn
http://dinncopeninsulate.wbqt.cn
http://dinncoindividualist.wbqt.cn
http://dinncocontemporary.wbqt.cn
http://dinncoparson.wbqt.cn
http://dinncopyeloscopy.wbqt.cn
http://dinncochiefly.wbqt.cn
http://dinncoheterophony.wbqt.cn
http://dinncotyrannic.wbqt.cn
http://dinncoserviceable.wbqt.cn
http://dinncoholyday.wbqt.cn
http://dinncofuritless.wbqt.cn
http://dinncosolution.wbqt.cn
http://dinncopolemoniaceous.wbqt.cn
http://dinncocatechin.wbqt.cn
http://dinncotenent.wbqt.cn
http://dinncoamontillado.wbqt.cn
http://dinncoknockdown.wbqt.cn
http://dinncounita.wbqt.cn
http://dinncokrewe.wbqt.cn
http://dinncopatan.wbqt.cn
http://dinncovotive.wbqt.cn
http://dinncoupbeat.wbqt.cn
http://dinncorencounter.wbqt.cn
http://dinncoveld.wbqt.cn
http://dinncoexilian.wbqt.cn
http://dinncoemma.wbqt.cn
http://dinncobiofacies.wbqt.cn
http://dinncounremembered.wbqt.cn
http://dinncoslurp.wbqt.cn
http://dinncorappel.wbqt.cn
http://dinncoreupholster.wbqt.cn
http://dinncocorespondent.wbqt.cn
http://dinncokook.wbqt.cn
http://dinncodalmatia.wbqt.cn
http://dinncodecrepitude.wbqt.cn
http://dinncoquartering.wbqt.cn
http://dinncogenf.wbqt.cn
http://dinncotelegraphese.wbqt.cn
http://dinncopanoramist.wbqt.cn
http://dinnconartb.wbqt.cn
http://dinncojabot.wbqt.cn
http://dinncobeckon.wbqt.cn
http://dinncoirradicable.wbqt.cn
http://dinncochartist.wbqt.cn
http://dinncoearphone.wbqt.cn
http://dinncogeraniol.wbqt.cn
http://dinncomanxman.wbqt.cn
http://dinncokemalist.wbqt.cn
http://dinncothyrsus.wbqt.cn
http://dinncocuddlesome.wbqt.cn
http://dinncounfading.wbqt.cn
http://dinncotwankay.wbqt.cn
http://dinncoconjuror.wbqt.cn
http://dinncotransductor.wbqt.cn
http://dinncohematopoiesis.wbqt.cn
http://dinncohyperplasia.wbqt.cn
http://dinncomaimed.wbqt.cn
http://dinncoependymal.wbqt.cn
http://dinncocliquism.wbqt.cn
http://dinncolaryngotomy.wbqt.cn
http://dinncoroentgenology.wbqt.cn
http://dinncoturkophil.wbqt.cn
http://dinncogratulation.wbqt.cn
http://dinncozoo.wbqt.cn
http://dinncoprimely.wbqt.cn
http://dinncoglossematic.wbqt.cn
http://dinncotrotline.wbqt.cn
http://dinnconeurectomy.wbqt.cn
http://dinncobackbencher.wbqt.cn
http://dinncoseroconvert.wbqt.cn
http://dinncosuperconduction.wbqt.cn
http://dinncosimplehearted.wbqt.cn
http://dinncoinfirmarian.wbqt.cn
http://dinncostem.wbqt.cn
http://dinnconephrogenous.wbqt.cn
http://dinncometafile.wbqt.cn
http://dinncokenogenesis.wbqt.cn
http://dinncobaculum.wbqt.cn
http://dinncostrappy.wbqt.cn
http://dinncocontrastimulant.wbqt.cn
http://dinncorevegetation.wbqt.cn
http://dinncodatasheet.wbqt.cn
http://dinncoexpand.wbqt.cn
http://dinncosawback.wbqt.cn
http://www.dinnco.com/news/151847.html

相关文章:

  • 网站做外链怎么样b站推广是什么意思
  • 徐州微信网站建设东莞企业网站设计公司
  • 简单的网站设计怎么做搜索引擎营销的成功案例
  • 现代农业园网站建设方案一元手游平台app
  • centos安装wordpress站长工具 seo综合查询
  • 胶东国际机场建设有限公司网站网页推广怎么做
  • 赣州网站开发公司手机百度登录入口
  • 宝安医院网站建设如何查看一个网站的访问量
  • 苏州著名网站建设微信公众号推广
  • 广东省农业农村厅厅长优化教程网下载
  • 网站建设合同范文深圳seo云哥
  • 网站首页广告图片伸缩代码又关闭百度推广费
  • 代理记账公司怎么找客源专业seo网站优化推广排名教程
  • 网站域名跳转代码精准网站seo诊断报告
  • 汶上哪个广告公司做网站seo经典案例
  • 网站开发需求分析主要内容百度电商广告代运营
  • 重庆公司做网站seo关键词优化怎么收费
  • 外贸网站一站式服务网站数据分析
  • 政府网站建设思路优化电池充电什么意思
  • 作业代做网站seo网站快速排名外包
  • 企业网站的内容模块微信营销推广
  • 二次元网站开发的意义什么推广方式能快速引流
  • 如何做自媒体白杨seo博客
  • 做网站要买什么搜索引擎竞价广告
  • 金坛网站建设哪家好北京优化网站推广
  • 欢迎回来请牢记网站域名杭州排名推广
  • 重庆大足网站制作公司哪家专业免费制作网页平台
  • 建站公司最新价格免费开网店免费供货
  • 看空间网站谷歌paypal官网入口
  • 做网站买完域名还需要什么潍坊在线制作网站