Multipart/form-data with two parts does not generate correct typescript
#1,078 opened on 2017年11月27日
説明
I have a C# method (prototype below) that contains one Object to be serialized to JSON and one file upload. No matter what option I try (JSON object as Form Data or as Body data), the JSON object is either not valid or not part of the generated typescript. This is using version 11.12.9.
When using [FromData] to incorporate the object as a multipart form, the generated typescript calls toString() on the object, which uses the Object prototype and therefore adds [object Object] to the call, which is not valid.
When using [FromBody] for the object, the object is entirely ignored by the generated typescript. It is part of the method signature but is never added to the HTTP request.
Providing method signature, Swagger JSON, and output Typescript below for reference.
Prototype
[HttpPost]
[AllowAnonymous]
[Consumes("multipart/form-data")]
[ActionName("applyForJob")]
[SwaggerResponse((int)HttpStatusCode.OK, null)]
[SwaggerResponse((int)HttpStatusCode.BadRequest, typeof(void))]
public async Task<IActionResult> ApplyForJob(
[FromForm] JobApplicationModel jobModel,
[FromForm] IFormFile resume)
Swagger
"/api/Contact/applyForJob": {
"post": {
"tags": [
"Contact"
],
"operationId": "Contact_applyForJob",
"consumes": [
"multipart/form-data"
],
"parameters": [
{
"type": "object",
"name": "jobModel",
"in": "formData",
"x-schema": {
"$ref": "#/definitions/JobApplicationModel"
},
"x-nullable": true
},
{
"type": "file",
"name": "resume",
"in": "formData",
"x-nullable": true
}
],
"responses": {
"200": {
"description": ""
},
"400": {
"description": ""
}
},
"security": [
{
"apiKey": []
}
]
}
},
Typescript
apiContactApplyforjob(jobModel: JobApplicationModel | null, resume: FileParameter | null): Observable<void> {
let url_ = this.baseUrl + "/api/Contact/applyForJob";
url_ = url_.replace(/[?&]$/, "");
const content_ = new FormData();
if (jobModel !== null && jobModel !== undefined)
content_.append("jobModel", jobModel.toString());
if (resume !== null && resume !== undefined)
content_.append("resume", resume.data, resume.fileName ? resume.fileName : "resume");
....[snipped for clarity]