Search code examples
javascriptnode.jsreactjssuperagent

How to get Data received in response superagent


I am trying to hit an api and want to get the data received in response.

For this purpose I am using superagent, I am getting the data from the api and I have checked in my network tab,but the problem is, I want the data that I got in my response network tab,but I am getting the whole network tab data instead of only response data.Here below is my code

  check=(evt)=>{

    evt.preventDefault();

    agent
        .get(`https://mws.amazonservices.com/Finances/2015-05-01`)
        .query({
            AWSAccessKeyId:'AKIAIOSFODNN7EXAMPLE',
            Action:'GetServiceStatus',
            SellerId:'A13LAO8KHSSL',
            MWSAuthToken:'533644733019',
            SignatureVersion:2,
            Timestamp:'2019-05-16T05:55:43Z',
            Version:'2015-05-01',
            Signature:'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
            SignatureMethod:'HmacSHA256'
        })
        .then(res => {
           console.log('here is the response');
            console.log(res)  // here it print the whole data I only need data got in //response
      })
      };

How can I only get data of response


Solution

  • SuperAgent returns an object with the request and response details, for example the status and text. If you want to have the raw response body, use the text property:

    agent
        .get(...)
        .query(...)
        .then(response => {
            const rawBody = response.text;
            console.log(rawBody);
        });
    

    Output:

    <?xml version="1.0"?>
    <GetServiceStatusResponse>
       ...
    </GetServiceStatusResponse>