快把我逼疯了!在以下每个截图中,同样的帖子请求在失眠症中也能很好地工作:

唯一的标题失眠症是:Content-Type: application/json。
现在,代码中的相同请求(I甚至复制了axios 从失眠生成的代码)通过类型记录中的axios:
const saveReqConfig: AxiosRequestConfig = {
method: 'POST',
url: 'THE SAME URL USED IN Insomina',
timeout: 3000,
data: {
name: `TestName`,
uri: `TestURI`,
statusCode: '200',
simulatedLatency: '0',
contentType: "application/json",
tags: '',
response: 'testing...',
type: 'VA',
},
headers: {
'Content-Type': 'application/json',
}
}
const normalAxios = axios.create();
const test = await normalAxios.request(saveReqConfig);不明白为什么我要从代码中获得AxiosError: Request failed with status code 400,但是同样的请求在失眠症中工作得很好。
发布于 2022-10-14 10:43:11
我认为您没有正确设置标头,或者您可能没有正确地设置.create()。
就像这样:
const instance = axios.create({
url: '/post',
baseURL: 'https://httpbin.org',
method: 'POST',
timeout: 1000,
headers: {
Content-Type: 'application/json' // <- set your headers
}
});
let res = await instance.request({ // <- pass the data here
data: { // This should be whatever you want to post to this url. I just copied what you had.
name: `TestName`,
uri: `TestURI`,
statusCode: '200',
simulatedLatency: '0',
tags: '',
response: 'testing...',
type: 'VA',
}
});您确定需要使用.create()工厂吗?像这样的普通职位也许能更好地满足你的需求?
const data= { title: 'Axios POST Request Example' };
const headers = {
Content-Type: 'application/json'
};
axios.post('url', data, { headers }).then(response => console.log(response.data.title);发布于 2022-10-23 11:13:02
张贴在这里以防对别人有帮助。结果发现,我无法以编程方式发布请求,原因是缺少TLS证书。我不知道失眠症可以选择禁用TLS,这就是为什么它在失眠症中起作用。禁用TLS (不要在生产中这样做!)从带有axios的节点创建一个axios实例,其中https代理将rejectedUnauthorized设置为false。
const instance = axios.create({
httpsAgent: new https.Agent({
rejectedUnauthorized: false
})
});
Also, set the environment variable as:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';https://stackoverflow.com/questions/74067906
复制相似问题