NAV 2009 SP1 - GET data from an API using 'Microsoft XML, v6.0'.XMLHTTP60

Please would someone be able to help point me in the right direction with this?
I am using NAV 2009 SP1 but need to GET data from an API using 'Microsoft XML, v6.0'.XMLHTTP60
The API also needs an API Key to be used and this key has been made available to me.
So far I have a codeunit which has the following global variables.

Name DataType Subtype Length
xmlHttpReq Automation 'Microsoft XML, v6.0'.XMLHTTP60
locautXmlDoc Automation 'Microsoft XML, v6.0'.DOMDocument60
XMLNode Automation 'Microsoft XML, v6.0'.IXMLDOMNode

In the codeunit I have the following code which is supposed to get the data from the API and collect the data as an XML file to be saved in a folder. It is difficult to tell whether or not any of it is working. I have used the Postman application to check the URL and it does reach the customer's API, but it cannot get through security. The codeunit runs but there is no data in the file which gets created. Also it is not clear to me whether or not the username and password parameters of the GET Action can be the inserted as I have done it, using the shop ID and the API Key. I suspect that these have to be passed through in another way?

IF ISCLEAR(locautXmlDoc) THEN
CREATE(locautXmlDoc);

IF ISCLEAR(xmlHttpReq) THEN
CREATE(xmlHttpReq);

xmlHttpReq.open('GET', 'https://marketplace-uat.harveynichols.com/api/orders', FALSE, [Shop-ID], [APIKey]);

locautXmlDoc.load(xmlHttpReq.responseXML);

locautXmlDoc.save('C:\temp\output.XML');

Any feedback on this would be very welcome indeed.
Many thanks.

Answers

  • ftorneroftornero Posts: 158Member
    Hello @robwalker425 ,

    You could test the conection with an external tool like Postman (https://www.getpostman.com/) to see if it works, and in the other hand you should call xmlHttpReq.send after xmlHttpReq.open, even with a empty string like this
    xmlHttpReq.send('');
    
  • robwalker425robwalker425 Posts: 2Member
    Hi ftornero
    Really appreciate your response!
    POSTMAN:
    Yes, I have tried to connect with Postman and it does work. Originally there was an issue with the API Key and other parameters which had to be set but this has been resolved and a file can be got. The Header data that had to be added are these:
    'Accept','application/xml'
    'Authorization','[our API KEY]'
    'Authorization','https://marketplace-uat.harveynichols.com'
    NAV:
    From NAV, you are right about the SEND call too and that has been added. Thanks.
    So now I can send the GET request and I get a response with a file but I still get an 'Not Authrorized' reply back. I also get an XML file which includes the error message and that is saved as requested as 'C:\temp\Output.xml'.
    So the new code is as follows:
    *************************
    IF ISCLEAR(locautXmlDoc) THEN
    CREATE(locautXmlDoc);
    locautXmlDoc.loadXML('C:\temp\input.xml');
    IF ISCLEAR(xmlHttpReq) THEN
    CREATE(xmlHttpReq);
    //xmlHttpReq.open('GET', 'https://marketplace-uat.harveynichols.com/api/orders', //FALSE, [Shop-ID], [APIKey]);
    xmlHttpReq.open('GET', 'https://marketplace-uat.harveynichols.com/api/orders');
    xmlHttpReq.setRequestHeader('Accept','application/xml');
    xmlHttpReq.setRequestHeader('Authorization','[API Key]');
    xmlHttpReq.setRequestHeader('Authorization','https://marketplace-uat.harveynichols.com');
    xmlHttpReq.send(locautXmlDoc);
    locautXmlDoc.load(xmlHttpReq.responseXML);
    locautXmlDoc.save('C:\temp\output.XML');
    *****************************
    Do you think the setRequestHeader in NAV should work like this to pass through the header information which was needed in Postman or should it be included somewhere in the original input.xml file which is sent? I have tried so many different possibilities that I do not know what else to try.....
    Your feedback would be truly appreciated.
    Thanks
  • ftorneroftornero Posts: 158Member
    Hello @robwalker425 .

    Yes, the setRequestHeader works in NAV like this.

    Could you try to do a new test after comment the second Authorization line, looks like is redundant.

    Regards
Sign In or Register to comment.