Content-Type 未添加到 axios post 请求标头中

问题描述 投票:0回答:1

我正在使用 axios 并尝试在 javascript 中发送以下格式的发布请求

let api_post_request = async (resourcePath, params, file, options) => {
let headers = {
        ...(getAccessToken()),
        'Content-Type' : 'multipart/form-data'
    };
    try{
        await axios({
            method: 'post',
            headers: headers,
            baseURL: `${Constants.PATH.BASE_URL}`,
            url: `${resourcePath}`,
            ...(params ? { params: params } : ""),
            ...(file ? {data : {file : file}} : "")
        });
    }

Authorization key 中设置了 Bearer token,但是在网络选项卡的请求标头中找不到 Content-Type。我使用的请求格式是否正确?因此,我收到 请求失败,状态代码 415 错误。

javascript post axios content-type
1个回答
0
投票

尝试这个 axios 文档示例 https://axios-http.com/docs/multipart

import axios from 'axios';

const form = new FormData();
form.append('my_field', 'my value');
form.append('my_buffer', new Blob(['some content']));

axios.post('https://example.com', form)

© www.soinside.com 2019 - 2024. All rights reserved.