【问题标题】:Api call works on postman but Doenst work on my codeApi 调用适用于邮递员,但不适用于我的代码
【发布时间】:2018-07-31 07:43:21
【问题描述】:

所以我必须更新一些用户信息,它在邮递员上运行良好,但是当我尝试在 react-native 中输入它时,我必须在 fetch 方法的主体中做错了什么。在邮递员中,我设置 x-www-form-urlencoded 并输入如下键:

键-----值

moto ----- 测试

这似乎可行,但是当我尝试对我的代码执行相同操作时,我以某种方式失败了,这是我的代码

updateUser(){

 return fetch(url,{
    method: "PATCH",
    headers: {
        "X-Auth-Token": bearerToken,
        "Content-Type":"application/x-www-form-urlencoded"
    },
      body: JSON.stringify({
                  moto: this.state.moto
          }
    })
  }
)

我得到 200 响应,这意味着调用有效,但我必须以某种方式设置参数 moto 错误。 有什么想法吗?

【问题讨论】:

    标签: reactjs api react-native fetch postman


    【解决方案1】:

    "Content-Type":"application/x-www-form-urlencoded"

    应该是

    “内容类型”:“应用程序/json”

    【讨论】:

      【解决方案2】:

      form-urlencoded 与您的body: JSON.stringify() 完全不同。

      您需要使用 FormData 对象:

      const body = new FormData();
      body.append('moto', this.state.moto);
      fetch(url, {
        method: "PATCH",
        headers: {
          "X-Auth-Token": bearerToken,
          "Content-Type": "application/x-www-form-urlencoded"
        },
        body,
      })
      

      【讨论】:

      • 我尝试了您的代码,但仍然没有。调用已完成,但参数必须输入错误。我将向您发布 console.log(body),也许您可​​以解决这个问题。 FormData[_parts: Array(1)}=> 部分 : Array(1) => 0:"moto" 1:test
      【解决方案3】:
      APICall = () => { 
        fetch(‘Your http URL’, {
          method: 'PATCH',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          ‘X-Auth-Token’: bearerToken,
          },
          body: JSON.stringify({ 
            moto: this.state.moto
          }) 
        }).then((response) => response.json())
            .then((responseJson) => {      
          if(responseJson.statuscode == 1) {
            Alert.alert('Success');
          } else {
            Alert.alert(responseJson.message);
          }
          }).catch((error) => {
            console.error(error);
          });
      }
      

      【讨论】:

      • 使用此函数调用您的 API。它对我有用。
      • 它对我不起作用,因为我需要调用的 API 必须使用以下标头: 'Content-Type': 'application/x-www-form-urlencoded' ,因此问题变成了什么我可以将我的身体以什么形式传递给 fetch 方法的请求。我相信解决方案接近@AKX 的方法
      • 尝试使用 'Content-Type': 'multipart/form-data' 而不是 'Content-Type': 'application/x-www-form-urlencoded'
      • 也试过了,结果还是一样。我想我需要以“Content-Type”的正确形式传递我的身体:“application/x-www-form-urlencoded”。你知道我如何将一个字符串传递给这个标题的有效形式吗?
      • 假设你有 3 个参数必须像这样传递 --> body: "firstName=Nikhil&favColor=blue&password=easytoguess"
      【解决方案4】:

      最后通过设置body来修复它

         body: 
            `moto=${this.state.moto}`
      

      似乎 urlencoded 标头需要以下形式的参数 参数1=值1&参数2=值2

      【讨论】:

        【解决方案5】:
        componentDidMount() {
        return fetch(“Your URL”, {
            method: 'post',
            headers: {
                "Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
                "Authorization": “token”
            },
            body: "firstName=Nikhil&favColor=blue&password=easytoguess"
        })
            .then((response) => response.json())
            .then(function (data) {
                alert(“Success”)
                console.log('Request succeeded with JSON response', data);
            })
            .catch(function (error) {
                alert("error occur")
                console.log('Request failed', error);
            });
        }
        

        【讨论】:

          【解决方案6】:
          const formData = new FormData();
          
          formData.append('email', 'test@gmail.com');
          formData.append('password', '123456');
          
          fetch("https://test.com/api/login", {
              method: 'post',
              body: formData
          })
          .then(res => res.json())
          .then(
          (result) => {
              console.log(result);
          }).catch(err => {
              console.log(err);
          })
          

          【讨论】:

            猜你喜欢
            • 2019-01-20
            • 1970-01-01
            • 1970-01-01
            • 2019-07-03
            • 2021-05-29
            • 2018-03-17
            • 1970-01-01
            • 2020-05-06
            • 2019-11-09
            相关资源
            最近更新 更多