I am making some mock up for the company I work to, I am making an API call to send an XML file but the client throws Uncaught TypeError: Object prototype may only be an Object or null: undefined
just before sending the request. I read that the imports are the problem but I can’t figure it out what, even because my company bought a very crappy tool to auto generate the server and client codes, the models, the api functions ecc ecc… but it sucks.
This is the call where the client fails:
upload() {
console.log('mando richiesta 1');
const fattura = this.delegates.parser.PostFile(this.file).toPromise().then((value) => {console.log(value)});
console.log(fattura);
}
the this.delegates.parser.PostFile(x)
is a function generated by the tool I mentioned before and I didn’t wrote it. It contains:
public PostFile(xmlFile: any) {
return this.request<IPostFileResponse>('POST', 'PostFile', { xmlFile }, false);
}
the tool-generated request<T>(x)
function:
protected request<TResponse>(method: 'DELETE' | 'GET' | 'HEAD' | 'JSONP' | 'OPTIONS' | 'POST' | 'PUT' | 'PATCH', resource: string, payload: any, disableContextAttach = false) {
const url = this.api + resource;
const options: IShHttpRequestBodyOptions<TResponse> = {};
const serialized = this.serializer.serialize(payload);
if (method.startsWith('P')) {
options.headers = { 'Content-Type': 'application/json' };
options.body = serialized;
} else {
if (typeof payload === 'object') {
for (const key in payload) {
const element = payload[key];
if (isNoU(element)) {
delete payload[key];
}
}
}
options.params = payload;
}
if (this.activity && this.activity.taskId && method !== 'JSONP') {
options.headers = {
'Content-Type': 'application/json',
'Activity-TaskId': this.activity.taskId
};
}
if (options) {
options.observe = 'response' as 'body';
options.responseType = 'text' as 'json';
}
let request: Observable<TResponse> = this.http.request<TResponse, any>(method, url, options)
.pipe(map(x => this.deserialize.call(this, x)));
if (!disableContextAttach) {
if (payload) {
this.attach(payload);
}
request = request.pipe(map(this.attach.bind(this)));
}
return request;
}
and server side I have:
[HttpPost("postFile")]
// <custom:customAttributesPostFile>
// </custom:customAttributesPostFile>
public async Task<ActionResult<PostFileResponse>> PostFile([FromForm] PostFileRequest request, CancellationToken requestAborted)
{
// <custom:PostFile>
XmlSerializer ser = new XmlSerializer(typeof(FatturaElettronicaTypeDTO));
Stream stream = (request.XmlFile as IFormFile).OpenReadStream();
FatturaElettronicaTypeDTO deserialized = (FatturaElettronicaTypeDTO)ser.Deserialize(stream);
_fatturaSelezionata.fatturaSelez = deserialized;
var fatturaDto = _fatturaMapper.mapFattura(deserialized);
return new PostFileResponse() { Fattura = fatturaDto }; /*fatturaDto*/
// </custom:PostFile>
}