【问题标题】:React Native - Axios POST with urlencoded paramsReact Native - 带有 urlencoded 参数的 Axios POST
【发布时间】:2019-04-07 14:49:43
【问题描述】:

我通过 Postman 成功触发了 POST 请求以检索 mobileSession 密钥。但是当我从 React Native 应用程序(通过 Axios)尝试相同的操作时,我得到了一些参数丢失的错误。有人可以根据 Postman 的要求告诉我 Axios 有什么问题吗?

邮递员:

和axios代码:

export function getMobileSession() {
    let requestOptions = {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    };

    let body = {
        username: 'myusername',
        password: 'mypw',
        api_key: 'apikey',
        api_sig: 'signature',
        method: 'auth.getMobileSession',
        format: 'json'
    };

    return axios.post('Lastfm_API_URL', JSON.stringify(body), requestOptions)
        .then(response => {
            return response;
        })
        .catch(err => {
            throw err;
        });
}

【问题讨论】:

    标签: react-native post parameters axios urlencode


    【解决方案1】:

    试试这个,

      return axios.post(`https://ws/audioscrobbler.com/2.0/`, JSON.stringify(body), requestOptions)
        .then(response => {
            return response;
        })
        .catch(err => {
            throw err;
        });
    

    更多信息请参考here了解反勾号。

    【讨论】:

    • 不适用于JSON:stringify(body),但适用于qs 包。 import qs from 'qs' && qs.stringify(body)
    猜你喜欢
    • 2019-05-18
    • 2019-08-09
    • 1970-01-01
    • 2022-01-11
    • 2018-01-07
    • 2021-07-12
    • 2019-02-28
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多