Here is a code snippet that highlights the problem:
import axios, { AxiosRequestConfig } from "axios";
function combineRequestParams(defaultParams: AxiosRequestConfig, params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig {
return {
...defaultParams,
...params1,
...(params2 || {}),
headers: {
...defaultParams,
...(params1.headers || {}),
...((params2 && params2.headers) || {}),
},
};
}
(async () => {
try {
const client = axios.create({
transformRequest: [(data, headers) => { return data; }], // Omitting this line causes: Invalid character in header content ["transformRequest"]
transformResponse: [(data, headers) => { return data; }], // Not including this line results in: Invalid character in header content ["transformResponse"]
baseURL: "https://localhost:7001/",
});
const requestParams = combineRequestParams(client.defaults, {}, {})
client.request({
...requestParams,
headers: {
"Content-Type": "application/json",
...requestParams.headers,
},
params: {date:'2020-03-31'},
responseType: 'json',
url: '/overview',
});
} catch (error) {
console.log(error);
}
})();
The error message you may encounter is:
(node:60845) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["adapter"]
at ClientRequest.setHeader (_http_outgoing.js:529:3)
at new ClientRequest (_http_client.js:241:14)
at Object.request (https.js:314:10)
at RedirectableRequest._performRequest (/Users/kamz/workspace/clarity/clarity-model/node_modules/follow-redirects/index.js:262:24)
at new RedirectableRequest (/Users/kamz/workspace/clarity/clarity-model/node_modules/follow-redirects/index.js:60:8)
at Object.request (/Users/kamz/workspace/clarity/clarity-model/node_modules/follow-redirects/index.js:458:14)
at dispatchHttpRequest (/Users/kamz/workspace/clarity/clarity-model/node_modules/axios/lib/adapters/http.js:195:25)
at new Promise (<anonymous>)
at httpAdapter (/Users/kamz/workspace/clarity/clarity-model/node_modules/axios/lib/adapters/http.js:46:10)
at dispatchRequest (/Users/kamz/workspace/clarity/clarity-model/node_modules/axios/lib/core/dispatchRequest.js:52:10)
(node:60845) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:60845) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.