The Way Of Life: February 2023
Google

Sunday, February 26, 2023

How to send an Image(base64) to an API with Visual Basic

If from my last article, I explained how to make an API Call using Visual Basic. Today I will explain how to send an image to an API with Visual Basic.

The common way to send an image to an API is by using base64 encoding. If you don’t know what is base64 encoding is, you can see it here first.

There are so many ways to encode images in base64 format. But in this article, I’m using Visual Basic 6.0 to encode the image.

Without further ado, let’s read the step below.

First You can download the example base64 VB class here.

With that source code, you can try to upload an image. But that base64 class that I’ve downloaded has a “bug” that causes an error when we send an image to an API.

Here’s why?






That class is generating Enter characters or Carriage Return/CR characters causing an error because base64 encoding results are “dirty”. We can use Ms. Word to see these characters

So, the solution is we should remove all CR characters with Replace function on VB6.

Replace(Bas64.Base64Buf, vbCrLf, "")

You can add this code below to my source code that you can download here.

Base64StringImage = """" & Replace(Bas64.Base64Buf, vbCrLf, "") & """"

sEntityBody = "{" + vbCrLf + """reference_id"":99," + vbCrLf

sEntityBody = sEntityBody & """photo_image""" & ":" & Base64StringImage & vbCrLf & "}"


Good luck and hope this will be useful.


Software :

Windows 10 Pro

Ms. Visual Basic 6.0


Labels: , ,

Saturday, February 11, 2023

API Call Using Visual Basic 6 (JSON Response)


Today. I’m gonna show you how to make API (Application Programming Interface) Call using Microsoft Visual Basic 6. And the first step that you have to do is install Visual Basic, I’m using Visual Basic 6 by the way.

First, you must create Visual Basic Project and then go to Project – References , Add Microsoft XML v6.0 and then click OK.


Make a Procedure let say GetRequest


Public Sub GetRequest

Dim xmlhttp As New MSXML2.XMLHTTP60

Dim url as String

Dim Response as String


‘set url


url  = https://api.yourwebsite.com

xmlhttp.Open “POST”, url, False


‘set header

xmlhttp.setRequestHeader "Host:", "api.your.api.url.destionation.com"

xmlhttp.setRequestHeader " Key:", "xyz"

xmlhttp.setRequestHeader " Token:", "$2a$10$ "

xmlhttp.setRequestHeader "Origin:", "http://web.com"

xmlhttp.setRequestHeader "Content-Type", "application/json"


‘set body

sEntityBody = "{" + vbCrLf + """reference_id"":999," + vbCrLf

sEntityBody = sEntityBody & """image""" & ":" & Base64StringImage & vbCrLf & "}"

xmlhttp.send " " + sEntityBody


‘if status 200 ( success )

If xmlhttp.status = "200" Then

    Response = xmlhttp.responseText

    Set xmlhttp = Nothing

When we receive a response from server than we must parse JSON with JSON Parser

    Set p = JSON.parse(Response)

JSON parser for Visual Basic you can download from here and add to Project Modules.

If we want to extract data from JSON we can user object pointer like below :

Text1.text = P.item(“data” .Item("province").Item("raw")

If we want to extract data from members of the item we can code like this below :

For I = 1 To p.Item("data").Item("member").Count()

p.Item("data").Item("member")(I).Item("name")

Next I

End if


Hope this article help you.

Download Source Code API Call VB 6 here 

Software :

Microsoft Visual basic 6.0

JSON Parser  Class Library

Operating System : 

Windows 10 Pro


related links :

Labels: , ,