Create an image file from an HTTP Request responsebody?

andrewmrichards

Registered User.
Local time
Today, 15:39
Joined
Mar 15, 2012
Messages
18
Morning all!

I'm working on a database which is designed to connect to an app called Canvas (www dot gocanvas dot com). It's for a client which runs a team of heating engineers. All the information regarding the engineer's job is uploaded to Canvas through their API as an XML file, and the engineers use an app on tablet to view the job details. When the engineer goes to their customer and does some work, the completed info (which parts were serviced etc) is then downloaded as an XML file via the API into the database. Also, the customer signs on the engineer's tablet to confirm that the work has been done.

Although the XML files for the job data upload and download fine, there's a different API for getting a download of the JPEG which contains the customer signature, and I'm having real problems finding out how to use this.

The API guide says this:
----
The Submissions API is restricted to authenticated users and requires a username and apassword to access. In addition, the ID field is required. If these fields aren't specified, an error is returned.

The result of this webservice invocation will be either a standard JPEG image or an error code.

Example:

htt ps : / / www . gocanvas . com / apiv2 / images . xml? image_id=1 & username= test @ gocanvas .com &password=test

----

All my code (which I've pasted at the foot of this message seems to work fine. There are no errors, and the ResponseBody object appears to contain a byte stream which would be the jpeg... if only I could get it into a jpeg file!

Does anyone have any clue how I go about taking this "stuff" that's come back in the responsebody and actually create a jpeg image file from it? I feel that I'm so close that I can smell it, but can't get the last step!

Many thanks for any help.

Andrew

Here's the existing code (spaces inserted into URL to allow me to post!):

Code:
 Sub DownloadImageFile()
Dim xhr As Object
Dim webServiceURL As String
Dim actionType As String
Dim PostData As String
Dim strResult As String
Dim blnSuccess As Boolean
Dim strDownloadedFilePath As String

Set xhr = CreateObject("Microsoft.XMLHTTP")

webServiceURL = "htt ps : // ww w. gocanvas .com/ apiv2 /images.xml? username={our-username}& password={our-password}&image_id={1234}"

xhr.Open "POST", webServiceURL, False
xhr.setRequestHeader "Content-Type", "image/jpeg"
xhr.setRequestHeader "Content-Length", Len(PostData)
xhr.Send PostData

blnSuccess = (xhr.Status = 200)
If blnSuccess Then
    strResult = xhr.responsebody 'xhr.responsebody does now appear to contain a byte stream

Else
    MsgBox "The signature could not be downloaded." & vbNewLine & vbNewLine & _
    "The message receieved from the server was: " & vbNewLine & xhr.Status & ": " & xhr.statusText
    strResult = xhr.responseText
End If

If blnSuccess Then

    'Save my image file here!

End If

Set xhr = Nothing

End Sub
 
I'm no expert in this area but your example is calling for a match on image ID and username but your code is looking for a username only
 
I'm no expert in this area but your example is calling for a match on image ID and username but your code is looking for a username only

Hi

Thanks for the input, but no - my code includes the parameter

Code:
&image_id=1234

You may need to scroll to the right to see it though!

Thanks
Andrew
 
ahm missed that but you've actually got

&image_id={1234}

Would that make a difference?

Also, perhaps wrong with the example but isn't text normally surrounded by quotes?

You could call this 'helpless help':confused:
 
ah missed that but you've actually got

&image_id={1234}

Would that make a difference?

Also, perhaps wrong with the example but isn't text normally surrounded by quotes?

You could call this 'helpless help':confused:
 

You are a star!!

Thank you so much - worked like a charm.

Unexpectedly, whether I set the filename to signature.png or signature.jpg, it still worked and happily saved a working image file!

Can I just ask a followup (probably being lazy now...) - I've seen various references in postings and other people's code to Freefile and Put and Open ... For x and Close in people's code, but never really understood how to use them. Any pointers to resources where I can learn more?

Anyway, thank you so much for your help with this!

Andrew
 
ah missed that but you've actually got

&image_id={1234}

Would that make a difference?

Also, perhaps wrong with the example but isn't text normally surrounded by quotes?

You could call this 'helpless help':confused:

:) Thanks for the help - of whatever kind!

But no - I just used the curly brackets as my way of saying "Actual value goes here..."

And the entire string is inside quotes, but as a set of parameter values to be submitted, they don't go in quotes.

Anyway, all sorted now!

The full working code, in case anyone would find it useful is as below. Note that the GetOptionValue method is a function which returns values from tblOptions within the database, as this is the way I store dynamically changeable settings for the database.

Code:
 Sub DownloadImageFile(ImageRef As String)
 Dim xhr As Object
 Dim webServiceURL As String
 Dim actionType As String
 Dim PostData As String
 Dim strResult As String
 Dim blnSuccess As Boolean
 
 Set xhr = CreateObject("Microsoft.XMLHTTP")
  webServiceURL = GetOptionValue("Canvas image download URL")
 webServiceURL = Replace(webServiceURL, "{Username}", "OurUsername")
 webServiceURL = Replace(webServiceURL, "{Password}", "OurPassword")
 webServiceURL = Replace(webServiceURL, "{Imageid}", ImageRef)
 
 xhr.Open "POST", webServiceURL, False
 
 xhr.setRequestHeader "Content-Type", "image/jpeg"
 xhr.setRequestHeader "Content-Length", Len(PostData)
 xhr.Send PostData
   
 blnSuccess = (xhr.Status = 200)
 
 If blnSuccess Then
   strResult = xhr.responsebody
 Else
   MsgBox "The signature image could not be downloaded." & vbNewLine & vbNewLine & _
       "The message receieved from the server was: " & vbNewLine & xhr.Status & _
        ": " & xhr.statusText, vbExclamation, cAppName
   
   strResult = xhr.responseText
 End If
 
If blnSuccess Then
    Dim bArray() As Byte
    Dim Fullname As String
    Dim FileNumber As Integer
     bArray = xhr.responsebody
    Fullname = GetOptionValue("Location for customer signature images") & "\" & ImageRef & ".jpg"
    
    FileNumber = FreeFile
    Open Fullname For Binary As FileNumber
    Put FileNumber, , bArray
    Close FileNumber
    
    Set xhr = Nothing
 End If
 End Sub
 

Users who are viewing this thread

Back
Top Bottom