I am trying to use VBA to Post to an API. I have code that I've used in the past (with other APIs) whose structure and formatting has worked. But it didn't this time, it seems to be the Send command that is the culprit. So I searched the web and found a few different samples of how to format the Send and I've tried all of them, but none of them work.
The attached code sample includes several of the ways I've tried to format. I've commented out all but one but left the others in comments to show what I've tried. As you take a look at the code, know that I've tried every combination of commented out code that I could.
Each time, I get a server response that "No file uploaded or URL or base 64 provided."
However, if I put the same variables into Postman and send it from there, it works fine, so I know it is not an error on the API side, it's the way I'm formatting the VBA. I've checked the data many times to make sure there are no typos.
I would appreciate any suggestions.
Sub macroPOST()
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Url = "https://api.ocr.space/parse/image"
objHTTP.Open "POST", Url, False
'objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "apikey", "helloworld"
objHTTP.setRequestHeader "Content-Type", "multipart/form-data"
'objHTTP.setRequestHeader "Content-Type", "image/png"
objHTTP.Send ("url=http://dl.a9t9.com/ocrbenchmark/eng.png&filetype=PNG&language=eng&isOverlayRequired=false&iscreatesearchablepdf=false&issearchablepdfhidetextlayer=false")
'objHTTP.Send ("url=http://dl.a9t9.com/ocrbenchmark/eng.png")
'objHTTP.Send ("url%3Dhttp%3A%2F%2Fdl%2Ea9t9%2Ecom%2Focrbenchmark%2Feng%2Epng")
BodyContent = "{" & Chr(34) & "url" & Chr(34) & ":" & Chr(34) & "http://dl.a9t9.com/ocrbenchmark/eng.png" & Chr(34) & "}"
'objHTTP.Send (BodyContent)
replyTXT = objHTTP.responseText
If objHTTP.Status = "200" Then 'success
MsgBox replyTXT
Else
MsgBox ("Problem")
End If
End Sub