Closed luisfpg closed 5 years ago
Requests of this type are still not sent correctly. It generates code like this:
const rb = new RequestBuilder(this.rootUrl, TokenService.ApiMainTokenPostPath, 'post');
if (params) {
rb.body(params.body, 'multipart/form-data');
}
, but the parameters need to be appended to the FormData object:
let formData: FormData = new FormData();
formData.append('id', model.id);
The request sent by Angular now carries these binary data https://imgur.com/a/slkSZ53, which cannot be read by the dotnet api.
@sudoman281 Can you attach the OpenAPI definition of the operation?
Sure, here it is:
"/api/Main/Token": {
"post": {
"tags": [
"Token"
],
"summary": "Vrací autentizační JWT token",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"required": [
"email",
"password"
],
"type": "object",
"properties": {
"email": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"encoding": {
"email": {
"style": "form"
},
"password": {
"style": "form"
}
}
}
}
},
"responses": {
"400": {
"description": "Neplatný požadavek",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"401": {
"description": "Špatné přihlašovací údaje",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"200": {
"description": "Přihlášení úspěšné"
}
}
}
}
I found that the solution for this is to change a method in one of the generated files - request-builder.ts. I needed to change
private formDataValue(value: any): any {
if (value === null || value === undefined) {
return null;
}
if (value instanceof Blob) {
return value;
}
if (typeof value === 'object') {
return new Blob([JSON.stringify(value)], { type: 'application/json' });
}
new Blob([String(value)], { type: 'text/plain' });
}
to
private formDataValue(value: any): any {
if (value === null || value === undefined) {
return null;
}
if (value instanceof Blob) {
return value;
}
if (typeof value === 'object') {
return new Blob([JSON.stringify(value)], { type: 'application/json' });
}
new value;
}
What is the reason for it to be made like it's now?
We shouldn't explicitly set the content type in this case.