Skip to content

Working with responses

Marco Spampinato edited this page Jul 22, 2018 · 4 revisions

Response header

As of commit #0835e7a responses are extended to include new Headers values:

response.Headers = {
                'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown',
                'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown',
                'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown',
                'x-mws-timestamp': res.headers['x-mws-timestamp'],
                'content-type': content_type || 'unknown',
                'content-charset' : charset || 'unknown',  //binaries only
                'content-length': res.headers['content-length'] || 'unknown',
                'content-md5' : res.headers['content-md5'],  //binaries only
                'date': res.headers['date'] || 'unknown', 
            }

there's still stuff to add and fix

Text and binaries responses handling

async function GetFeedSubmissionResult(FeedSubmissionId: string) {
    try {
        const amcli = new amazonMws(accessKey, accessSecret);
        const response = await amcli.feeds.search({
            'Version': '2009-01-01',
            'Action': 'GetFeedSubmissionResult',
            'SellerId': SELLER_ID,
            'MWSAuthToken': MWS_AUTH_TOKEN,
            'FeedSubmissionId': FeedSubmissionId
        });
        /* check for binaries */
        if(response.Headers['content-type'].indexOf('application') > -1)
            StuffWithBinaries(response.data);
        else
            StuffWithObjects(response);
    }
    catch(e){
        throw e;
    }  
}

Whenever the object has a content-type that includes 'application/xyz', the library will throw that buffer in response.data, with response.Header you can get extended informations about the data, such as content-type,content-charset and content-md5.

Force raw responses

As of last updates, I had to extends the options you could provide to the request JSON: __RAW__ : boolean Allows you to always have a raw response as buffer in the response.data field. __CHARSET__ : string Allows you to set the charset, normally it would get it automatically as the header's content-type could have the charset=xyz parameter, and it is fixed as now, though free to set whatever you want. Please refer to Iconv's manual for the supported charset. Use like this:

async function GetFeedSubmissionResult(FeedSubmissionId: string) {
    try {
        const amcli = new amazonMws(accessKey, accessSecret);
        const response = await amcli.feeds.search({
            'Version': '2009-01-01',
            'Action': 'GetFeedSubmissionResult',
            'SellerId': SELLER_ID,
            'MWSAuthToken': MWS_AUTH_TOKEN,
            'FeedSubmissionId': FeedSubmissionId,
            '__RAW__' : true, // yay we handle the data
        });
        /* work with your binary */
        console.log(response.Headers);
        writeFileSync('data.bin',response.data);
    }
    catch(e){
        throw e;
    }  
}