mockserver icon indicating copy to clipboard operation
mockserver copied to clipboard

JSON Object not getting returned in response

Open abax1 opened this issue 6 years ago • 2 comments
trafficstars

I'm trying to return a json object but there is no response data coming back. It works fine over postman but not when using axios. Can someone help me with this?

My frontend code looks like this:-

export const getCollaborator = (collaboratorID, dispatch) => {
  axios
    .get(`http://localhost:8081/collaborators/${collaboratorID}`,
    {
      headers:{
        "Accept": "application/json"
      }
    })
    .then(
      // success
      ({ data, status }) => {
        console.log("status", status);
        
        return dispatch({
          type: GET_COLLABORATOR,
          payload: data
        });
      }
    ).catch(error => {
      if (error.response) {
        // The request was made and the server responded with a status code
        // that falls out of the range of 2xx
        console.log(error.response.data);
        console.log(error.response.status);
        console.log(error.response.headers);
      } else if (error.request) {
        // The request was made but no response was received
        // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
        // http.ClientRequest in node.js
        console.log(error.request);
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log('Error', error.message);
      }
      console.log('====================================');
      console.log(error);
      console.log('====================================');
      console.log(error.config);
    });
};

And the mock file:-

HTTP/1.1 200 OK
Content-Type: application/json

#import './data.json';

data.json:-

{
  "collaborator_id": 1,
  "username": "[email protected]",
  "profile_desc": "I am amazing",
  "primary_expertise": 1,
  "secondary_expertise": 3,
  "primary_other": "",
  "secondary_other": "",
  "weblinks": [
    {
      "type": "personal",
      "url": "www.my-site.com"
    },
    {
      "type": "linkedIn",
      "url": "www.linkedIn.com"
    }
  ],
  "skills": [
    {
      "skill_name": "java",
      "skill_level": 1
    },
    {
      "skill_name": "React",
      "skill_level": 2
    },
    {
      "skill_name": "Python",
      "skill_level": 3
    }
  ],
  "education": [
    {
      "school_name": "Cambridge University",
      "country": 1,
      "qualification": "B. Sc.",
      "major": "1st Class",
      "year": 1999
    },
    {
      "school_name": "Oxford University",
      "country": 1,
      "qualification": "B. Sc.",
      "major": "1st Class",
      "year": 2000
    }
  ]
}

abax1 avatar Jan 28 '19 23:01 abax1

Log of the response / error with axios? :)

odino avatar Jan 29 '19 07:01 odino

I'll try and reproduce it and send the error log. I have tried the same call using json-server and it works fine. Very odd. Not sure what I am doing wrong. Would be grateful if someone could try and reproduce in case it is my environment that is causing an issue.

It was returning Network Error with no status code. It goes straight here:-

.catch(error => {
      if (error.response) {
        // The request was made and the server responded with a status code
        // that falls out of the range of 2xx
        console.log(error.response.data);
        console.log(error.response.status);
        console.log(error.response.headers);
      } else if (error.request) {
        // The request was made but no response was received
        // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
        // http.ClientRequest in node.js
        console.log(error.request);
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log('Error', error.message);/* <-- code executes here */
      }
      console.log('====================================');
      console.log(error);
      console.log('====================================');
      console.log(error.config);
    });

abax1 avatar Jan 29 '19 09:01 abax1