Home > Blockchain >  Error in API response saying required parameters React native
Error in API response saying required parameters React native

Time:10-02

When I call API I am getting below error in response. please find below is code and error message. TEST RESPONSE:

{ 
  "responseData": {"limit": ["Limit is required"], 
  "module_type": ["Module type required"], 
  "section": ["section value \"liveability || investment || recommend\" is required"], 
  "skip": ["Skip is required"]
}

Implemented code:

fetch( 'https://api.dotcomkart.com/api/homePagePropertyList?', {
    method: 'POST',

    body: JSON.stringify({
    skip: 0,
    limit: 10,
    module_type:'buy',
    section: 'liveability'
    }),
  })

CodePudding user response:

Sometimes when you work with REST API call you have to work with correct headers. In your case I suppose your are missing two important headers required to activate a good communication between client and servers:

  • accept
  • content-type

Please review your code based on this one:

fetch('https://api.dotcomkart.com/api/homePagePropertyList?', {
    method: 'POST',
    headers: {
      "accept": "application/json",
      "content-type": "application/json"
    },
    body: JSON.stringify({
      skip: 0,
      limit: 10,
      module_type:'buy',
      section: 'liveability'
    }),
  })

I think the server is returning "missing" parameters because is not able to understand the type of content. With Content-Type you should be able to instruct the server on how to parse your data.

CodePudding user response:

Try this way

import FormData from 'FormData';
...
var data = new FormData();

data.append("skip", "0");
data.append("module_type", "buy");
....
fetch('YOUR_URL', {
method: 'POST',
headers: {
    Accept: 'application/json',
    'Content-Type': 'multipart/form-data',
},
body: data,
})
.then((response) => response.json())
.then((responseJson) => {
    console.log('response object:',responseJson)
})
.catch((error) => {
  console.error(error);
});
  • Related