我在Swagger工具中比较新。我尝试使用 swagger编辑器测试我的 Restfull 应用程序。我使用基本身份验证来访问Web服务。
在 Swagger-UI 中,预览看起来正确,即内容类型:application/json
,json在正文中。但是当我从Swagger编辑器发送GET请求时服务器,我收到了一个错误。
ERROR Server not found or an error occurred
My Swagger
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore (Simple)",
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
"termsOfService": "http://helloreverb.com/terms/",
"contact": {
"name": "Swagger API team",
"email": "abc@gmail.com",
"url": "http://avfg.com"
},
"license": {
"name": "MIT",
"url": "http://opensource.org/licenses/MIT"
}
},
"host": "127.0.0.1:8xxx",
"basePath": "/v1",
"schemes": [
"http"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/facedetect/{username}/{albumname}/{imagename}": {
"get": {
"description": "Returns all pets from the system that the user has access to",
"operationId": "findPets",
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"name": "username",
"in": "path",
"description": "tags to filter by",
"required": true,
"type": "string"
},
{
"name": "albumname",
"in": "path",
"description": "maximum number of results to return",
"required": true,
"type": "string"
},
{
"name": "imagename",
"in": "path",
"description": "maximum number of results to return",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "pet response",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/pet"
}
}
},
"default": {
"description": "unexpected error",
"schema": {
"$ref": "#/definitions/errorModel"
}
}
}
}
}
},
"definitions": {
"pet": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"errorModel": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
请帮帮我。
提前致谢。
答案 0 :(得分:1)
我得到了解决方案。
其CORS问题。我的浏览器阻止了cors请求。我安装了 Chrome扩展程序,可将 Access-Control-Allow-Origin 添加到外发请求中。
答案 1 :(得分:0)
确保您的服务器正在运行。
如果你安装了招摇,你可以
npm config set tag-version-prefix ''
答案 2 :(得分:0)
我有类似的问题
这是CORS,但是:
在nodeJS中我已设置:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, UPDATE, DELETE, OPTIONS')
res.header("Content-Type", "application/json");
next();
});
但是它仅在请求未经授权(api-key)时才有用。为了使它起作用,我必须更改和使用:
const cors = require('cors');
app.use(cors());
希望这会有所帮助