1

I have this code in Visual Basic .net and I would like to make the same thing in Visual Basic 6. The code upload an image to a given URL. I have to use Rest protocol because the server I upload the image request it.

 Dim res As New RestSharp.RestRequest("URL_OF_SERVER", RestSharp.Method.POST)

 res.AddFile("file", "File_location_on_PC")
 Dim restClient As New RestSharp.RestClient()

 Dim r As New RestSharp.RestResponse
 r = restClient.Execute(res)

Is it posible to do the same Rest Protocol in Visual Basic 6?

EDIT 1: I HAVE TRIED THIS BUT AN ERROR HAPPENS

Dim xmlhttp As MSXML2.XMLHTTP30
Const STR_BOUNDARY  As String = "3fbd04f5-b1ed-4060-99b9-fca7ff59c113"
Dim nFile           As Integer
Dim baBuffer()      As Byte
Dim sPostData       As String

'--- read file
nFile = FreeFile
Open NombreArchivo For Binary Access Read As nFile
If LOF(nFile) > 0 Then
    ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
    Get nFile, , baBuffer
    sPostData = StrConv(baBuffer, vbUnicode)
End If
Close nFile
'--- prepare body
sPostData = "--" & STR_BOUNDARY & vbCrLf & _
"   Content-Disposition: form-data; name=""image002""; filename=""C:\image002.jpg" & _
"--" & STR_BOUNDARY & "--"
'--- post
Set xmlhttp = New MSXML2.XMLHTTP30
With xmlhttp
    .Open "POST", Url, False
    .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
    .send pvToByteArray(sPostData)
End With

Private Function pvToByteArray(sText As String) As Byte()
   pvToByteArray = StrConv(sText, vbFromUnicode)
End Function

ERROR :

responseText
{"message":"file.not_found","error":"There is no file in request.","status":400,"cause":[]}

Thanks!

2 Answers 2

3

If you need just to post a file here is a simple function

Private Sub pvPostFile(sUrl As String, sFileName As String, Optional ByVal bAsync As Boolean)
    Const STR_BOUNDARY  As String = "3fbd04f5-b1ed-4060-99b9-fca7ff59c113"
    Dim nFile           As Integer
    Dim baBuffer()      As Byte
    Dim sPostData       As String

    '--- read file
    nFile = FreeFile
    Open sFileName For Binary Access Read As nFile
    If LOF(nFile) > 0 Then
        ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
        Get nFile, , baBuffer
        sPostData = StrConv(baBuffer, vbUnicode)
    End If
    Close nFile
    '--- prepare body
    sPostData = "--" & STR_BOUNDARY & vbCrLf & _
        "Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
        "Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
        sPostData & vbCrLf & _
        "--" & STR_BOUNDARY & "--"
    '--- post
    With CreateObject("Microsoft.XMLHTTP")
        .Open "POST", sUrl, bAsync
        .SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
        .Send pvToByteArray(sPostData)
    End With
End Sub

Private Function pvToByteArray(sText As String) As Byte()
    pvToByteArray = StrConv(sText, vbFromUnicode)
End Function

You can switch to MSXML2.ServerXMLHTTP to be able to use its SetTimeouts method if your file is large enough and timeouts in 30 sec.

Sign up to request clarification or add additional context in comments.

3 Comments

responseText {"message":"file.not_found","error":"There is no file in request.","status":400,"cause":[]} That's what it says... :(
I have edited the post with the new code I use.. Please check it!.. THANKS!
In original code you use res.AddFile("file", "File_location_on_PC") where file is the name of the form element, so match this in Content-Disposition: form-data; name=""file""... unless there is a parameter image002 we are not aware of.
1

You can create a COM callable wrapper (CCW) for the RestSharp assembly - see this URL for details of how to do this:

http://msdn.microsoft.com/en-us/library/ms973802.aspx

Once it is registered, you can use RestSharp from VB6.

1 Comment

I did wrap RestSharp in a COM (target runtime 4.7.2), and it worked, except one issue, if the COM is used in a Win32 application (For example, an application written in Delphi 2007), it by default cannot access websites which only enabled TLS 1.2 (works with websites which also enabled TLS 1.0), but if the same COM is used by an .Net application, it works with both TLS 1.0 and TLS 1.2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.