Home > Net >  Axios returning binary data instead of XML
Axios returning binary data instead of XML

Time:12-01

Been spending the past few hours figuring out why Axios is doing this.

I tried to do this in the request library in nodeJS and it works fine, but Axios isn't.

Essentially what i'm doing is sending a request of XML data:

 var options = {
        'method': 'POST',
        'url': 'https://rvices.svc',
        'headers': {
          'Content-Type': 'text/xml; charset=utf-8',
          'SOAPAction': 'http://etProject'
        },
        data: xmlData};

with XMLData looking simliar to :

 let xmlData = `<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://p" xmlns:pws="http://"

etc etc (it has mostly private data in it so I stripped most of it out).

when I try to use axios to get this

const testData = await axios(options)

i get returned a whole lot of code that looks like this:

    '���_\x1B�\f�10�\x17�Ч�{O�\x00��5h�S�������\x0F���s�( Ғ�\x0F�����m�\x15\x01\x13��6b��\x06%\x00��\x15p8<;��W�4����\x0B���\x01���e�\x7FvZ�{���Ï������\x06��-�z��\x01}�!�r�A�\x13��\x11O�w6ũ���{�\x03����;{����\x01\x7Fy��KoՎ���\x1Bߚe��W��mЇ�qD�a��[�7Ӄ���@��F<\x1C/mF�{\x03�h��#�\x16�\x11\x1F\x1F�L9\x0FM\x8A\x0E�\x
17�h���\x03�4�7�f=bj*8�p�\x13_�\x17�5���_�Ӑ�|M>����\r��F�8q�iE�#��\x0E?�v�������O�xq3���x�Q�튱\x1F?G&HG9��6���V\x1B⫯Ev\x01rc\x13\x10�\'�7��`�Ii��x�~LM6�#˒74#@�����f�*\x7F\x16(5|\x1CWl��\x07\t\x1F��z�\x15\x00\x1B��4�\x13���LCTG�\x1FI�����\fec�h\x02�~��i`�:Ғ�\x0F���y\b#�]V��g��Ӈ�\x14|���6~\x19~c`�/�O���M\x01��k\x
10�\' ���\x07S\r?|��T�A�\x0FӒ�\x0F��ܷ\'.s�!>�tbX\x05�\fs\x18�\r�"`���\x10lV٠\x05@ܲ�\x02\x0E\x07h���\n'  
    '���[�7}�>54 r�����ʦ\x15�\x17��\x0E:

that is the right amount of characters (100k ) but jumbled

compared to doing this with request which returns the xml back I expect ala:

</b:ProjectTaskTypeDetail></b:PwsProjectTaskTypeElement><b:PwsProjectTaskTypeElement><b:ProjectTaskTypeDetail><b:ExternalSystemIdentifier i:nil="true"/><b:ProjectTaskTypeId i:nil="true"/><b:ProjectTaskTypeUid>5776</b:ProjectTaskTypeUid><b:ProjectTaskTypeName>Faon</b:Proj
ectTaskTypeName>

one thing I noticed is axios is breaking my request up into multiple lines like this:

 '<com:PwsProjectRef><com:ProjectCode>201268</com:ProjectCode></com:PwsProjectRef>\n'  
      '\n'  
      '<com:PwsProjectRef><com:ProjectCode>210115-01</com:ProjectCode></com:PwsProjectRef>\n'  
      '\n'  

even though there's no \n's in my request or breaks like that.

So i'm wondering if anyone has ran into this before and knows how to solve it?

Request is working but request (from what I can tell?) doesn't work with asynch code (i'm probably wrong about this)

Sorry for the vagueness!

CodePudding user response:

You should be using the responseType config option to set the expected response which reflects the Accept HTTP header and not the Content-Type one:

 const options = {
    method: 'POST',
    url: 'https://rvices.svc',
    headers: {
      'Content-Type': 'text/xml; charset=utf-8',
      'SOAPAction': 'http://etProject'
    },
    data: xmlData,
    responseType: 'document',
    responseEncoding: 'utf8'
};

const testData = await axios(options);

CodePudding user response:

try with this code

Save as get-data.js file

const axios = require("axios");

const getData = async () => {
    try {
        const resp = await axios.get('your xml URL',
            {
                headers: {
                    'Accept-Encoding': 'application/xml',
                }
            }
        );
        console.log(resp.data);
    } catch (err) {
        // Handle Error Here
        console.error(err);
    }
};

getData()
npm install axios
node get-data.js
  • Related