After creating an Angular2 typescript client using the Swagger Editor found at http://editor.swagger.io, I encountered an issue where POST endpoints were not functioning properly. Despite setting up all API endpoints successfully, any POST requests failed to reach the server.
The specific method in the generated typescript code appeared correct as the headers were being set accurately. However, upon closer inspection, it seemed that the issue lied within the this.http.request
call itself.
public snapshotsFoo (data: Array<number>, extraHttpRequestParams?: any ) : Observable<models.Snapshot> {
const path = this.basePath + '/Snapshots/foo';
let queryParameters = new URLSearchParams();
let headerParams = this.defaultHeaders;
headerParams.set("Content-Type", "application/json");
// verify required parameter 'data' is not null or undefined
if (data === null || data === undefined) {
throw new Error('Required parameter data was null or undefined when calling snapshotsFoo.');
}
let requestOptions: RequestOptionsArgs = {
method: 'POST',
headers: headerParams,
search: queryParameters
};
requestOptions.body = JSON.stringify(data);
console.log('I am in foo', path, requestOptions);
return this.http.request(path, requestOptions)
.map((response: Response) => {
if (response.status === 204) {
return undefined;
} else {
return response.json();
}
});
}
Interestingly, while the endpoint worked seamlessly on the swagger ui (), POSTman, and even via cURL commands, the same functionality seemed to be missing when using the generated typescript code. No errors were thrown, yet nothing was posted to the server.