Hi, I am having an issue with trying to get the PDF downloaded. The code I am using is VBA in Excel.
All my other API calls to get basic company information and filing history seem to work.
The filing history document response shows me 2 links, the “self” and the “document link” which are below:
I use the following code to get the response:
baseUrl = “https://api.company-information.service.gov.uk”
url = baseUrl & “/company/” & companyNumber & “/filing-history”
Set xmlHttp = New MSXML2.XMLHTTP60
xmlHttp.Open "GET", url, False
xmlHttp.setRequestHeader "Authorization", "Basic " & Base64Encode(API_KEY & ":")
xmlHttp.send
I then pickup the url from the response data. Partial response shown below as this is embedded in the filing history.
“links”:{“self”:“https://document-api.company-information.service.gov.uk/document/iEfD-fTMmUChdIs_8YviXdGNrHKdO4rDspJauabod-0",“document”:"https://document-api.company-information.service.gov.uk/document/iEfD-fTMmUChdIs_8YviXdGNrHKdO4rDspJauabod-0/content”
As soon as I use the document url I get a “permission denied object” in the xmlHttp.send command and I assume this is becuase the document link does not exist but why do I not get the 302 redirect response which will redirect me to the Amazon web server. Why does adding “/content” cause this error ?
pdfUrl = https://document-api.company-information.service.gov.uk/document/iEfD-fTMmUChdIs_8YviXdGNrHKdO4rDspJauabod-0/content
Dim xmlHttp As MSXML2.XMLHTTP60
Set xmlHttp = New MSXML2.XMLHTTP60
xmlHttp.Open "GET", pdfUrl, False
xmlHttp.setRequestHeader "Authorization", "Basic " & Base64Encode(API_KEY & ":")
xmlHttp.setRequestHeader "Accept", "application/pdf"
xmlHttp.send
'Handle redirect to Amazon web server where the actual file is located
If xmlHttp.status = 302 Then
pdfUrl = xmlHttp.getResponseHeader("Location")
Set xmlHttp = New MSXML2.XMLHTTP60
xmlHttp.Open "GET", pdfUrl, False
xmlHttp.send
End If
'check request was ok
If xmlHttp.status = 200 Then
'Do rest of code
End If