ehmad11 / netsuite-rest

Make requests to Oracle NetSuite REST Web Services.
MIT License
50 stars 18 forks source link

some of the response is gzip-ed #3

Closed charles20hk closed 3 years ago

charles20hk commented 4 years ago

In the latest version of Netsuite API, some of the (error) response becomes gzip-ed

"response": { "statusCode": 400, "body":"�\b�]�AO�0\f���\n+����i�ހ�\f\n��R�DJ�a�\u000bմ�N:N~zϟ�|R2P�ʋ�����η:Ѿz�$ɥ�}��m���׌����!�~�F7j�$H\n��\f~N�R\+�vS�k�Z$J�C�!�����.X�$�}�\b�9d�,r�ȡ�;pi�x.Q9��a�C�\"��g�����#���&��X�aC�y���k���南�μw�<u~;_}�LɈ+", "headers": { "... "content-encoding": "gzip", "content-type": "application/vnd.oracle.resource+json; type=error; charset=UTF-8", ....

However, I couldn't include "gzip": true as a param in request function and allow it to be used in "requestPromise"

request(opts) { const { path = '*', method = 'GET', body = '' } = opts;

    ...
    const options = {
        uri: uri,
        method,
        resolveWithFulLResponse: true,
        transform: (body, response) => {
            let data = {}
            if (body)
                data = JSON.parse(body)
            return {
                statusCode: response.statusCode,
                'headers': response.headers,
                'data': data
            };
        }
    };
    options.headers = this.getAuthorizationHeader(options);
    if (body) {
        options.body = body;
        options.headers.prefer = "transient";
    }
    return requestPromise(options);
}
ehmad11 commented 4 years ago

i think gzip should not be a problem and i am not getting any gziped response on any bad requests, can you share more details, request path, method and body?

also make sure you have a catch block because non-2xx will be rejected

charles20hk commented 4 years ago

this is part of the code for creating a contact, the err in catch shows the above response

netsuite.request({ path: 'record/v1/contact/', method: 'POST', body: JSON.stringify(contact) }) .then(response => { ... }) .catch(err => { return({'status': -1, "error": err}); });

if I include gzip: true

const options = { uri: uri, method, resolveWithFulLResponse: true, transform: (body, response) => { let data = {} if (body) data = JSON.parse(body) return { statusCode: response.statusCode, 'headers': response.headers, 'data': data }; }, gzip: true }; options.headers = this.getAuthorizationHeader(options);

I can get the response in plain text

    "name": "StatusCodeError",
    "statusCode": 400,
    "message": "400 - \"{\\\"type\\\":\\\"https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1\\\",\\\"title\\\":\\\"Bad Request\\\",\\\"status\\\":400,\\\"o:errorDetails\\\":[{\\\"detail\\\":\\\"Error while accessing a resource. A contact record with this name already exists. Every contact record must have a unique name.\\\",\\\"o:errorCode\\\":\\\"USER_ERROR\\\"}]}\\n\"",
    "error": "{\"type\":\"https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1\",\"title\":\"Bad Request\",\"status\":400,\"o:errorDetails\":[{\"detail\":\"Error while accessing a resource. A contact record with this name already exists. Every contact record must have a unique name.\",\"o:errorCode\":\"USER_ERROR\"}]}\n",
ehmad11 commented 3 years ago

I have now set gzip:true for all the requests, tests passed, patch published on npm

thank you @charles20hk