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

3
  • 1
    I do not work with curl so I am not sure if this will help? Commented Mar 21, 2020 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 ... Commented Mar 21, 2020 at 11:17
  • 1
    Nah I am not interested in curl. Just dropped in to help you out with the link :) Commented Mar 21, 2020 at 11:22

1 Answer 1

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"



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

10 Comments

That's great. How can I get the JSON response?
@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.
Thanks a lot. Can you give me an example of exporting to file?
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"
@yasser I don't know. I guess you could check every second for the response file to been created
|

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.