Javascript 使用主体数据删除Axios
我使用axios调用删除请求并传递文档中定义的正文数据,但我的请求没有添加此请求 在此之前,当我使用HTTP头但不在正文中传递参数时,我能够使用axis在没有任何参数的情况下调用delete请求 我的要求是像使用postman一样从服务器调用DeleteHTTP请求 这是使用jest(测试框架)的代码,我需要一些帮助Javascript 使用主体数据删除Axios,javascript,axios,jestjs,Javascript,Axios,Jestjs,我使用axios调用删除请求并传递文档中定义的正文数据,但我的请求没有添加此请求 在此之前,当我使用HTTP头但不在正文中传递参数时,我能够使用axis在没有任何参数的情况下调用delete请求 我的要求是像使用postman一样从服务器调用DeleteHTTP请求 这是使用jest(测试框架)的代码,我需要一些帮助 beforeEach(() => { jest.resetModules(); process.env = { JWT_SECRET: '123Abc123', TE
beforeEach(() => { jest.resetModules(); process.env = { JWT_SECRET: '123Abc123', TEST_URL: testURL }; });
it('Should delete a product with valid id (success scenario)', async done => {
console.log ("******* id to be deleted: " + productIdToDelete );
axios.interceptors.request.use(request => {
console.log('Starting Request', request)
return request
})
const data = {
id: 'productIdToDelete'
}
axios.delete(getURL('/product/delete'), {headers, data})
.then((res) => {
expect(res.status).toBe(200);
expect(res.data.data.token).toBeDefined();
})
.catch((err) => {
throw new Error('Test Failed');
})
.then(() => {
done();
})
});
});
当我执行此代码时,请求具有以下有效负载:
Starting Request {
url: 'http://localhost:3000/product/delete',
method: 'delete',
headers: {
common: { Accept: 'application/json, text/plain, */*' },
delete: {},
get: {},
head: {},
post: { 'Content-Type': 'application/x-www-form-urlencoded' },
put: { 'Content-Type': 'application/x-www-form-urlencoded' },
patch: { 'Content-Type': 'application/x-www-form-urlencoded' },
Authorization: 'Bearer paperboy'
},
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
adapter: [Function: httpAdapter],
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus]
}
我举了几个例子,一点运气都没有。你们有什么建议吗