Skip to content

Commit 924a602

Browse files
ipopov001timhall
authored andcommitted
Store converted body as Variant
Update WebHelpers and WebRequest to allow ConvertToFormat to return byte arrays rather than only a string, to be able to use it to upload binary data, e.g. images Closes #146
1 parent a61dc3f commit 924a602

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/WebHelpers.bas

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,10 @@ Public Sub LogRequest(Client As WebClient, Request As WebRequest)
475475
Debug.Print "Cookie: " & web_KeyValue("Key") & "=" & web_KeyValue("Value")
476476
Next web_KeyValue
477477

478-
If Request.Body <> "" Then
479-
Debug.Print vbNewLine & Request.Body
478+
If Not IsEmpty(Request.Body) Then
479+
Debug.Print vbNewLine & CStr(Request.Body)
480480
End If
481-
481+
482482
Debug.Print
483483
End If
484484
End Sub
@@ -740,10 +740,10 @@ End Function
740740
' @param {Dictionary|Collection|Variant} Obj
741741
' @param {WebFormat} Format
742742
' @param {String} [CustomFormat] Name of registered custom converter
743-
' @return {String}
743+
' @return {Variant}
744744
' @throws 11001 - Error during conversion
745745
''
746-
Public Function ConvertToFormat(Obj As Variant, Format As WebFormat, Optional CustomFormat As String = "") As String
746+
Public Function ConvertToFormat(Obj As Variant, Format As WebFormat, Optional CustomFormat As String = "") As Variant
747747
On Error GoTo web_ErrorHandling
748748

749749
Select Case Format
@@ -761,9 +761,9 @@ Public Function ConvertToFormat(Obj As Variant, Format As WebFormat, Optional Cu
761761
Set web_Converter = web_GetConverter(CustomFormat)
762762
web_Callback = web_Converter("ConvertCallback")
763763

764-
If web_Converter.Exists("web_Instance") Then
764+
If web_Converter.Exists("Instance") Then
765765
Dim web_Instance As Object
766-
Set web_Instance = web_Converter("web_Instance")
766+
Set web_Instance = web_Converter("Instance")
767767
ConvertToFormat = VBA.CallByName(web_Instance, web_Callback, VBA.vbMethod, Obj)
768768
Else
769769
ConvertToFormat = Application.Run(web_Callback, Obj)

src/WebRequest.cls

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Private web_pResponseFormat As WebFormat
5656
Private web_pCustomRequestFormat As String
5757
Private web_pCustomResponseFormat As String
5858
Private web_pBody As Variant
59-
Private web_pConvertedBody As String
59+
Private web_pConvertedBody As Variant
6060
Private web_pContentType As String
6161
Private web_pAccept As String
6262
Private web_pContentLength As Long
@@ -230,7 +230,7 @@ Public Property Let RequestFormat(Value As WebFormat)
230230
web_pRequestFormat = Value
231231

232232
' Clear cached converted body
233-
web_pConvertedBody = ""
233+
web_pConvertedBody = Empty
234234
End If
235235
End Property
236236

@@ -266,7 +266,7 @@ Public Property Let ResponseFormat(Value As WebFormat)
266266
web_pResponseFormat = Value
267267

268268
' Clear cached converted body
269-
web_pConvertedBody = ""
269+
web_pConvertedBody = Empty
270270
End If
271271
End Property
272272

@@ -298,7 +298,7 @@ Public Property Let CustomRequestFormat(Value As String)
298298
web_pCustomRequestFormat = Value
299299

300300
' Clear cached converted body
301-
web_pConvertedBody = ""
301+
web_pConvertedBody = Empty
302302

303303
If Value <> "" Then
304304
web_pRequestFormat = WebFormat.Custom
@@ -334,7 +334,7 @@ Public Property Let CustomResponseFormat(Value As String)
334334
web_pCustomResponseFormat = Value
335335

336336
' Clear cached converted body
337-
web_pConvertedBody = ""
337+
web_pConvertedBody = Empty
338338

339339
If Value <> "" Then
340340
ResponseFormat = WebFormat.Custom
@@ -461,7 +461,7 @@ Public Property Get Body() As Variant
461461
If Not VBA.IsEmpty(web_pBody) Then
462462
If VBA.VarType(web_pBody) = vbString Then
463463
Body = web_pBody
464-
ElseIf web_pConvertedBody = "" Then
464+
ElseIf IsEmpty(web_pConvertedBody) Then
465465
' Convert body and cache
466466
Body = WebHelpers.ConvertToFormat(web_pBody, Me.RequestFormat, Me.CustomRequestFormat)
467467
web_pConvertedBody = Body
@@ -471,11 +471,11 @@ Public Property Get Body() As Variant
471471
End If
472472
End Property
473473
Public Property Let Body(Value As Variant)
474-
web_pConvertedBody = ""
474+
web_pConvertedBody = Empty
475475
web_pBody = Value
476476
End Property
477477
Public Property Set Body(Value As Variant)
478-
web_pConvertedBody = ""
478+
web_pConvertedBody = Empty
479479
Set web_pBody = Value
480480
End Property
481481

@@ -697,7 +697,7 @@ Public Sub AddBodyParameter(Key As Variant, Value As Variant)
697697
End If
698698

699699
' Clear cached converted body
700-
web_pConvertedBody = ""
700+
web_pConvertedBody = Empty
701701
End Sub
702702

703703
''

0 commit comments

Comments
 (0)