为什么在测试OpenAPI定义时,SwaggerHub返回错误403? [英] Why does SwaggerHub return error 403 when I test my OpenAPI definition?

查看:20
本文介绍了为什么在测试OpenAPI定义时,SwaggerHub返回错误403?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SwaggerHub上托管了以下OpenAPI定义:
https://app.swaggerhub.com/apis/MyFirstAPI/1.0.1-oas3

openapi: 3.0.0
servers:
  - url: http://api.myfirstapi.com/
info:
  version: 1.0.1-oas3
  title: Equ API
paths:
  /query:
    get:
      tags:
        - developers
      parameters:
        - in: query
          name: searchString
          schema:
            type: string
      responses:
        '200':
          description: search results matching criteria
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Equity'
        '400':
          description: There is 400
components:
  schemas:
    Equity:
      ...

当我测试GET /query请求时,它返回403错误:

content-length: 0 
date: Fri,10 Sep 2021 14:32:54 GMT 
server: nginx/1.18.0 + Phusion Passenger(R) 6.0.8 
status: 403 Forbidden 
via: 1.1 b5d86079363e9709b4c4051d3c94ac3d.cloudfront.net (CloudFront) 
x-amz-cf-id: pYbLwlrEHg5DXkGe5FkysAjSjbSDqUg7Rrhbv-Dt8Xwt8JuPRMAW3Q== 
x-amz-cf-pop: DEL54-C1 
x-cache: Error from cloudfront 
x-powered-by: Express,Phusion Passenger(R) 6.0.8

为什么会发生此错误以及如何修复它?

推荐答案

此403错误有点误导性。这里的实际问题是请求的目标服务器--api.myfirstapi.com--实际上并不存在。servers列表应指定您的真实服务器。

如果您正在设计新的API,并且还没有实时服务器,您可以使用SwaggerHub的mock server来模拟响应并测试API调用。

将模拟添加到您的API定义:

  1. 单击编辑器工具栏上的API名称。
  2. 切换到集成选项卡,然后单击添加新集成
  3. 从列表中选择API自动模拟,然后单击添加
  4. 名称输入任意值(例如模拟),保留其他选项不变,然后单击创建并执行
  5. 关闭其余对话框。

这将为您的API创建模拟服务器,并将其添加到您的API定义中的servers列表中:

servers: 
  - description: SwaggerHub API Auto Mocking
    url: https://virtserver.swaggerhub.com/OWNER_NAME/API_NAME/VERSION

在测试API调用之前,请确保在API文档中选择此服务器。

这篇关于为什么在测试OpenAPI定义时,SwaggerHub返回错误403?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆