我正在使用swagger 2.0来记录我的API。我定义了一个资源/ getsize,如下面的代码片段所示:
post:
operationId: users.getSize
tags:
- Users
summary: Measure your body
description: import a photo
consumes:
- multipart/form-data
parameters:
- in: formData
name: image1
type: file
required: true
- in: path
name: height
type: number
required: true
当我尝试从sawgger ui发送请求时,得到了预期的结果。但是,当我尝试使用Angluar组件访问此资源时会出现问题,这里是component.ts文件的代码片段:
onSubmit() {
const formData = new FormData();
formData.append('image1', this.image);
console.log(formData.getAll);
console.log(this.image);
this.http.post(endpoint,formData);
.subscribe(res => {
console.log(res);
alert('SUCCESS !!');
})
}
服务器响应为:
error:
detail: "Missing formdata parameter 'image1'"
status: 400
title: "Bad Request"
type: "about:blank"
有什么建议 ?
您可以使用URLSearchParams作为请求的主体来执行此操作,angular会自动将内容类型设置为application / x-www-form-urlencoded并对主体进行正确编码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句