【问题标题】:How to set basic auth, content-type and data to send in axios post request?如何设置基本身份验证、内容类型和数据以在 axios 发布请求中发送?
【发布时间】:2021-03-06 02:28:50
【问题描述】:

我是 React Native 的初学者,我正在尝试使用 axios 将数据发送到 IBM 语音到文本转换 Api。我在 Postman 中收到了响应,但我无法在 axios 请求中设置我的数据。该请求需要基本身份验证、内容类型和文件(数据)。如何在 axios 请求中设置所有这些? 我收到的状态码为 415 This is the code Picture

axios
      .post(
        IBMURL,
        {
          withCredentials: true,
          headers: {
            'Content-Type': 'audio/mp3',
          },
        },
        {
          auth: {
            username: 'apikey',
            password: ApiPassword,
          },
        },
      )
      .then((response) => {
        console.log('Reponse', response.data);
      })
      .catch((e) => console.log(e));

【问题讨论】:

    标签: javascript react-native http axios


    【解决方案1】:

    axios.post 需要url 之后的正文:

    axios.post(url, body, options);
    

    传递null 或空对象应该可以解决它。

    【讨论】:

    • 我确实尝试在第二个参数位置添加一个对象,但没有成功。请参阅上面的示例代码我已经将 3 个参数传递给一个对象
    • 你错了,标题的options参数应该是最后一个
    猜你喜欢
    • 2023-03-19
    • 2017-10-19
    • 2014-08-04
    • 1970-01-01
    • 2014-08-14
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多