axios transformRequest - how to alter JSON payload

牧云@^-^@ 提交于 2019-12-02 00:39:11

Wouldn't you want to JSON.stringify() your transformed post data? Like below:

const instance = axios.create({
    baseURL: 'api-url.com',
    transformRequest: [
        (data, headers) => {
            const encryptedString = encryptPayload(JSON.stringify(data));

            data = {
                SecretStuff: encryptedString,
            };

            return JSON.stringify(data);
        },
    ],  
});
axios.create({
    transformRequest: [(data, headers) => {
        // modify data here
        return data;
    }, ...axios.defaults.transformRequest]
});

have to append the original axios.defaults.transformRequest to the transformRequest option here..

To amend the values instead of override the output in the request I would do this:

const instance = axios.create({
baseURL: 'api-url.com',
transformRequest: [
    (data, headers) => {
        data.append('myKey','myValue');            
        return data;
    },
]
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!