2

I have registered in a website that supports API and I have tested the CURL command line and it works fine for me

curl --location --request POST "https://api.imgbb.com/1/upload?key=APIKEY" --form "image=iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="

this is API information page https://api.imgbb.com/

I have no great knowledge of how to use excel VBA to send an equivalent command line? I am totally new to such stuff and I have searched a lot and can't get how things go on

I have found this link that may be helpful but it is in VB.NET Help with Imgur API and VB.NET - Image POST

** Now I got the solution and I have developed it like that

Sub Test()
Dim v As Double, sPath As String, sAPIKey As String, sBase64 As String, cmd As String

sPath = ThisWorkbook.Path & "\Result.txt"
sAPIKey = "APIKEY"
sBase64 = ConvertFileToBase64(ThisWorkbook.Path & "\Logo.png")

cmd = Replace(Replace("curl --location --request POST ""https://api.imgbb.com/1/upload?key=¤"" --form ""image=$"" -o ", "¤", sAPIKey), "$", sBase64) & sPath
v = Shell(cmd)
Debug.Print cmd & " Completed" & vbCr & "Process " & v
End Sub

Public Function ConvertFileToBase64(strFilePath As String) As String
Const UseBinaryStreamType = 1

Dim streamInput: Set streamInput = CreateObject("ADODB.Stream")
Dim xmlDoc: Set xmlDoc = CreateObject("Microsoft.XMLDOM")
Dim xmlElem: Set xmlElem = xmlDoc.CreateElement("tmp")

streamInput.Open
streamInput.Type = UseBinaryStreamType
streamInput.LoadFromFile strFilePath
xmlElem.DataType = "bin.base64"
xmlElem.NodeTypedValue = streamInput.Read
ConvertFileToBase64 = Replace(xmlElem.Text, vbLf, "")

Set streamInput = Nothing
Set xmlDoc = Nothing
Set xmlElem = Nothing
End Function

This works fine for small sizes but not for the large ones ..example I have a Logo.png and the size is 545KB in size and this fails. While other smaller images uploaded well

I have manually tried it and it failed too. it seems cms window has a limit number of characters allowed so I can't get all the base64 string in the command line enter image description here

YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • 1
    I do not work with curl so I am not sure if [this](https://stackoverflow.com/questions/17063550/equivalent-curl-in-vba) will help? – Siddharth Rout Mar 21 '20 at 11:15
  • You can run the curl command line from the command prompt. Just register in the webite `imgbb.com` and you can get the free API key from this link `https://api.imgbb.com/`. then you can run the line I have posted ... – YasserKhalil Mar 21 '20 at 11:17
  • 1
    Nah I am not interested in curl. Just dropped in to help you out with the link :) – Siddharth Rout Mar 21 '20 at 11:22

1 Answers1

1

You can use Shell

Sub mycurl()

  Dim var As Double, cmd As String
  cmd = "curl -I ""https://api.imgbb.com/"" -o imgbb.txt" ' head only
  var = Shell(cmd)
  MsgBox cmd & " completed" & vbCr & "Process " & var

End Sub

Looks like the API accepts binary files so no need to base64 encode. Try

cmd = "curl --location --request POST ""https://api.imgbb.com/1/upload?" & _
      "key=" & sAPIKey & """ " & _
      "--form image=@""/path/Logo.png"" -o " & sPath & "/response.json"



CDP1802
  • 13,871
  • 2
  • 7
  • 17
  • That's great. How can I get the JSON response? – YasserKhalil Mar 21 '20 at 12:47
  • 1
    @Yasser That's a Curl question not VBA but -o filename saves the output to a file so if the response to your request is JSON I guess it will be in there. – CDP1802 Mar 21 '20 at 12:55
  • Thanks a lot. Can you give me an example of exporting to file? – YasserKhalil Mar 21 '20 at 13:01
  • I could see "imgbb.txt" but is it possible to change the path instead of Documents folder? – YasserKhalil Mar 21 '20 at 13:05
  • I can solve this point out by using `sPath = ThisWorkbook.Path & "\Result.txt"` then at the end ` -o " & sPath`. Thanks a lot for great help. – YasserKhalil Mar 21 '20 at 13:09
  • I have updated the main post. Can you have a look, please? – YasserKhalil Mar 21 '20 at 13:44
  • @Yasser You need to learn how to use the -F option in Curt to set form parameters from a file -F "image=@/path/filename" . See (https://stackoverflow.com/questions/12667797/using-curl-to-upload-post-data-with-files). You will have save your base64 encoding to a temporary file. – CDP1802 Mar 21 '20 at 14:03
  • 1
    I have a look but couldn't edit my own so as to fit. Can you give me an example based on the cmd line?. I tried this but failed `curl --location --request POST "https://api.imgbb.com/1/upload?key=APIKEY" --data-binary '@C:/Users/Future/Desktop/Output.txt"` – YasserKhalil Mar 21 '20 at 14:10
  • Thanks a lot. Last point, is there a way to make the code in VBA wait until the upload process complete? as I intend to upload multiple files. – YasserKhalil Mar 21 '20 at 15:08
  • 1
    @yasser I don't know. I guess you could check every second for the response file to been created – CDP1802 Mar 21 '20 at 15:15