I am trying to make a fetch() api call from typescript application based on below specification:
- Method: POST
- Request URL: https://cloud.memsource.com/web/api2/v1/projects/KmtNyVlz1skQd2aMVEipp7/jobs?token=AuthenticationToken
- (Header) Content-Disposition: filename*=UTF-8''file.txt
- (Header) Memsource: {"targetLangs":["de","fr"]}
- (Header) Content-Type: application/octet-stream
- Response: Job UID (e.g. dOYgeXzAdAbj4xFjuEVZP2)
I am getting following error when trying to use nested object/array value for headers in node-fetch call.
(property) RequestInit.headers?: HeadersInit Type '{ 'Content-Type': string; 'Content-Disposition': string; 'Memsource': { 'targetLangs': string[]; }; }' is not assignable to type 'HeadersInit'. Type '{ 'Content-Type': string; 'Content-Disposition': string; 'Memsource': { 'targetLangs': string[]; }; }' is missing the following properties from type 'Headers': forEach, append, delete, get, and 7 more.ts(2322) index.d.ts(45, 5): The expected type comes from property 'headers' which is declared here on type 'RequestInit'
Below is the line of the code giving the problem.
let project: Promise<Response> = await fetch(apiURL, {
method: 'POST',
headers: {
'Content-Disposition': 'd:/_export/en_US.json',
'Memsource': { 'targetLangs': ['fr_fr', 'es_es'] },
},
}).then(res => res.json())
.then(json => json)
.catch(err => console.log(err));
What should I do to fix the problem? Am I not allowed to use this notation?