我正在使用Swagger 2.0,我有一个问题是发送多个帖子参数.我有一个招摇错误Operation cannot have a body parameter and a formData parameter,我不知道如何解决它.在我的定义中,我有一个body参数,这个参数需要一个JSON格式,但是我还有其他参数,比如要上传的文件和文件名.
如何发送body和formData参数呢?
这是Web服务定义:
/updateDatas:
post:
summary: Upadate datas
description: |
Update datas
consumes:
- multipart/form-data
produces:
- application/json
parameters:
- name: firstFileName
in: formData
description: First file name.
required: true
type: string
- name: secondFileName
in: formData
description: Second file name.
required: true
type: string
- name: datas
in: body
description: Json object informations.
required: true
schema:
$ref: '#/definitions/Datas'
- name: firstFile
in: formData
description: First file .jpg …Run Code Online (Sandbox Code Playgroud) 我想获取每个路由上的所有 http 错误,而无需每次重写 if 400 then if 404 then if 500 then 等等...所以我ErrorHandler()在每个路由处理程序中都有一个函数:
func (h *Handler) List(c *gin.Context) {
movies, err := h.service.ListService()
if err != nil {
utils.ErrorHandler(c, err)
return
}
c.JSON(http.StatusOK, movies)
}
Run Code Online (Sandbox Code Playgroud)
这个函数看起来像这样:
func ErrorHandler(c *gin.Context, err error) {
if err == ErrNotFound {
// 404
c.JSON(http.StatusNotFound, gin.H{"error": ErrNotFound.Error()})
} else if err == ErrInternalServerError {
// 500
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError.Error()})
} // etc...
}
Run Code Online (Sandbox Code Playgroud)
ErrNotFound或者ErrInternalServerError只是像这样初始化的全局变量:
var ErrNotFound = errors.New(http.StatusText(http.StatusNotFound)) // 404 …Run Code Online (Sandbox Code Playgroud) 我将 Go 1.17 与 Gin 一起使用,我想在将数据发送到数据库之前实现结构验证。我从Gin 文档中获取了示例。
在结构中,我们可以声明不同的标签来验证字段,如下所示:
type User struct {
FirstName string `json:"first_name" binding:"required"`
LastName string `json:"last_name" binding:"required"`
Age uint8 `json:"age" binding:"gte=0,lte=130"`
Email string `json:"email" binding:"required,email"`
FavouriteColor string `json:"favourite_color" binding:"iscolor"`
}
Run Code Online (Sandbox Code Playgroud)
在处理程序中,我可以捕获如下错误:
var u User
if err := c.ShouldBindWith(&u, binding.Query); err == nil {
c.JSON(http.StatusOK, gin.H{"message": "Good Job"})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
Run Code Online (Sandbox Code Playgroud)
错误消息将是:
{
"error": "Key: 'User.FirstName' Error:Field validation for 'FirstName' failed on the 'required' tag\nKey: 'User.LastName' Error:Field validation for 'LastName' failed on …Run Code Online (Sandbox Code Playgroud) 我正在尝试将PDF文件附加到sendgrid发送的电子邮件中.
这是我的代码:
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("from@example.com")
subject = "subject"
to_email = Email("to@example.com")
content = Content("text/html", email_body)
pdf = open(pdf_path, "rb").read().encode("base64")
attachment = Attachment()
attachment.set_content(pdf)
attachment.set_type("application/pdf")
attachment.set_filename("test.pdf")
attachment.set_disposition("attachment")
attachment.set_content_id(number)
mail = Mail(from_email, subject, to_email, content)
mail.add_attachment(attachment)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
Run Code Online (Sandbox Code Playgroud)
但是Sendgrid Python库抛出错误HTTP Error 400:Bad Request.
我的代码出了什么问题?
我正在尝试使用此库初始化带有自动完成功能的两个输入.当我加载页面时,我将触发Ajax来初始化两个输入文本.
但我不知道当我的猫鼬发现完成时我怎么能发现.
这是我的服务器端代码:
app.post('/init/autocomplete', function(req, res){
var autocomplete = {
companies: [],
offices: []
};
// Find all companies
Company.find({}, function(err, companies) {
if (err) throw err;
companies.forEach(function(company) {
autocomplete.companies.push({value: company.name})
});
console.log('One');
});
// Find all offices
Office.find({}, function(err, offices) {
if (err) throw err;
offices.forEach(function(office) {
autocomplete.offices.push({value: office.name})
});
console.log('Two');
});
console.log('Three');
// res.json(autocomplete);
});
Run Code Online (Sandbox Code Playgroud)
我知道find方法是异步的.这就是为什么我按以下顺序看到我的console.log():
Three
One
Two
Run Code Online (Sandbox Code Playgroud)
如何完成并完成console.log('Three');时如何触发?Company.findOffice.find
我想看看console.log('Three');最后一个位置.
编辑:
我想我可以这样做:
app.post('/init/autocomplete', function(req, res){
var autocomplete = { …Run Code Online (Sandbox Code Playgroud) 当我在 /login 中时,我想将最后一条路由传递到我的路由器中,以便在登录到所需路由时重定向用户。
所以用户转到/payment我重定向到/login,当身份验证正常时,我想将用户重定向到payement
这是我的 router.js :
import ...
Vue.use(Router)
let router = new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/login',
name: 'login',
component: Login
},
{
path: '/about',
name: 'about',
component: About
},
{
path: '/payment',
name: 'payment',
component: Payment,
meta: {
requiresAuth: true
}
},
{
path: '/my-account',
name: 'my-account',
component: MyAccount,
meta: {
requiresAuth: true
}
}
]
})
router.beforeEach((to, from, …Run Code Online (Sandbox Code Playgroud) 我使用 VueJS 创建了一个渐进式 Web 应用程序,文件由 vuecli 生成。所以我有默认的 registerServiceWorker.js 我只是评论环境条件:
/* eslint-disable no-console */
import { register } from 'register-service-worker'
// if (process.env.NODE_ENV === 'production') {
register(`${process.env.BASE_URL}service-worker.js`, {
ready () {
console.log(
'App is being served from cache by a service worker.\n' +
'For more details, visit https://...'
)
},
registered () {
console.log('Service worker has been registered.')
},
cached () {
console.log('Content has been cached for offline use.')
},
updatefound () {
console.log('New content is downloading.')
},
updated () { …Run Code Online (Sandbox Code Playgroud) 我正在使用Phalcon PHP,我想第一次尝试socket.io.我用socket.io做了教程聊天消息.但现在我想在我的数据库中选择一些数据,用查询Phalcon计算表'product'中的行数:
$count_products = Product::count();
Run Code Online (Sandbox Code Playgroud)
例如,在我的HTML页面中,我有5个产品,当我将一个或多个产品添加到我的表产品中时,我想要自动刷新以查看我的HTML页面中的6个产品.
你能帮帮我吗?
我在VueJS应用程序中使用Axios,我想在请求中添加默认的GET参数。我通过URL发送API-KEY,但?api-key=secret我不想每次都指定此参数。
我在文档中看到可以设置Global自定义默认值。这样,我们不必每次都指定标题。但是我们可以这样做以获得参数吗?
我将PHP 7与Phalcon 3配合使用。我想在一个函数中实现Stripe响应。我可以处理类似的错误是。
这是代码:
try {
// Use Stripe's library to make requests...
} catch(\Stripe\Error\Card $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$err = $body['error'];
print('Status is:' . $e->getHttpStatus() . "\n");
print('Type is:' . $err['type'] . "\n");
print('Code is:' . $err['code'] . "\n");
// param is '' in this case
print('Param is:' . $err['param'] . "\n");
print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\RateLimit $e) {
// Too many requests …Run Code Online (Sandbox Code Playgroud) vue.js ×3
go ×2
go-gin ×2
javascript ×2
php ×2
axios ×1
mongodb ×1
mongoose ×1
mysql ×1
node.js ×1
parameters ×1
pdf ×1
phalcon ×1
promise ×1
python ×1
refactoring ×1
rest ×1
sendgrid ×1
socket.io ×1
swagger ×1
validation ×1
vue-router ×1
vuejs2 ×1