Upload file and post JSON together to an API - vba

I am developing a macro to compress PDF files, for this purpose I am using ILovePDF API.
https://developer.ilovepdf.com/docs/api-reference#upload.
I am just unable to figure out how I can POST JSON Data and Upload a file.
This is a snippet of code:
task = "task id here"
server = "url here"
token = "token here"
file_to_upload = "this is the real deal"
With CreateObject("MSXML2.ServerXMLHTTP")
'Upload
.Open "POST", "https://" & server & "/v1/upload", False
.setRequestHeader "Authorization", "Bearer " & token
.setRequestHeader "Content-type", "application/json"
.send ("{""task"": """ & task & """,""file"": """ & file_to_upload & """}")
As you can see, need to POST task and file. The task is a string and file is the PDF file stored in my local storage.
Can anyone help me with this?
Thanks!

Related

Upload file to onedrive personel via VBA REST API

There is simple code for upload file to dropbox.
Does someone have an example of a similar code for Onedrive
Function uploadSave(stm As ADODB.Stream, fname As String, token As String) As integer
Const lngTimeout = 890000
Dim http As WinHttp.WinHttpRequest
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
http.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout
http.Open "POST", "https://content.dropboxapi.com/2/files/upload", False
http.setRequestHeader "Content-Length", stm.Size
http.setRequestHeader "Authorization", "Bearer " & token
http.setRequestHeader "User-Agent", "api-explorer-client"
http.setRequestHeader "Content-Type", "application/octet-stream"
http.setRequestHeader "Dropbox-API-Arg", "{""path"":""" & fname & """,""mode"":{"".tag"":""overwrite""},""autorename"":true}"
http.Send (stm.Read)
Set stm = Nothing
uploadSave = http.Status
End Function

sending payload for post method in VBA

I'm trying to login to a website using VBA Excel, but I'm having trouble to send payload post method like in python request.
Example in python
def login():
s = requests.Session()
payload = {
'username':sett.username,
'password':sett.password1
}
res = s.post(link, json=payload)
ref_t = json.loads(res.content)['content']['refresh_token']
t = json.loads(res.content)['content']['token']
return t
this is what I've tried but fail:
Dim request As New WinHttpRequest
request.Open "POST", auth_url, False
auth = "username=" & user & "," & "password=" & pass
request.SetRequestHeader "content-type", "application/json;charset=UTF-8"
request.SetCredentials "username:" & user, "password:" & pass, 0
request.Send auth
If request.Status <> 200 Then
MsgBox request.ResponseText
Exit Sub
End If
the response text shows "Username is required, but was not received", "Password is required, but was not received", "code":400.
Edit:
Dim auth As New Dictionary
auth.Add "username", user
auth.Add "password", pass
request.Open "POST", auth_url, False
request.SetRequestHeader "content-type", "application/json"
request.Send JsonConverter.ConvertToJson(auth)
Turns out that I need to use SetRequestHeader
Edit:
Dim auth As New Dictionary
auth.Add "username", user
auth.Add "password", pass
request.Open "POST", auth_url, False
request.SetRequestHeader "content-type", "application/json"
request.Send JsonConverter.ConvertToJson(auth)

Pass Outlook variable data to API

API constants for example sendDataToApi = "mail_from=mail#mail.com&mail_to=mail#mail.com work. API receive data.
VBA code for connection with API:
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "https://url.xdomain.com/api/insert"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
sendDataToApi = "mail_from=mail#mail.com&mail_to=&mail2#mail.com"
objHTTP.Send sendDataToApi
What is the syntax for inserting variables to be sent?
For example strTo = objMail.To and add to sendDataApi.
This part of code adds me to API strTo instead of email.
sendDataToApi = "mail_from=strTo"

REST API access via VBA returns "Invalid ID/Key" error

I am attempting to access the Appointments-Plus.com API via VBA code and am consistently being told I'm giving it an invalid site-id/key. I'm using Access from the Office365 version.
This is the relevant documentation for this API call.
When I use Postman to test out the API, I am able to successfully connect and I get data back. The URL Postman puts together is this:
https://ws.appointment-plus.com/Locations/GetLocations?Authorization:Basic=<site-ID>:<Key>&response_type=xml
My VBA code is this:
Public Sub RESTtestBigURL()
Dim responseType As String
responseType = "response_type=json"
Dim restRequest As WinHttp.WinHttpRequest
Set restRequest = New WinHttp.WinHttpRequest
Dim restResult As String
With restRequest
.Open "POST", "https://ws.appointment-plus.com/Locations/GetLocations?Authorization:Basic=<site-ID>:<Key>&response_type=xml", False
.Send
.WaitForResponse
Debug.Print ".ResponseText: " & .ResponseText
Debug.Print ".Status: " & .Status
Debug.Print ".StatusText: " & .StatusText
Debug.Print ".ResponseBody: " & .ResponseBody
End With
End Sub
I know that the first question is "are you sure you've got the <site-ID> and <key> correct???" Yes - I've copy/pasted the entire URL from Postman into my VBA code, and I've had another couple of pairs of eyeballs review it to confirm that they're still the same.
When I run that code, I get:
.ResponseText: <?xml version="1.0" encoding="utf-8" ?>
<APResponse>
<resource>customers</resource>
<action>getcustomers</action>
<request></request>
<result>fail</result>
<count>0</count>
<errors>
<error><![CDATA[Web Services authentication failed: invalid Site ID or API Key]]></error>
</errors>
</APResponse>
I've tried several other methods of accessing the API, all of which are giving me the same "Invalid ID/Key" error:
Public Sub SecondRESTtestMSXML()
Dim restRequest As MSXML2.XMLHTTP60
Set restRequest = New MSXML2.XMLHTTP60
With restRequest
.Open "GET", URL & REQUEST_GET_LOCATIONS, True
.SetRequestHeader "Authorization", "Basic" & SITE_ID & ":" & API_KEY
.SetRequestHeader "response_type", "xml"
.SetRequestHeader "Accept-Encoding", "application/xml"
.Send "{""response_type"":""JSON""&""location"":""582""}"
While .ReadyState <> 4
DoEvents
Wend
Debug.Print ".ResponseText: " & .ResponseText
Debug.Print ".Status: " & .Status
Debug.Print ".StatusText: " & .StatusText
Debug.Print ".ResponseBody: " & .ResponseBody
End With
End Sub
There is a suggestion that this is a duplicate of another question that was resolved by Base64-encoding. However, this method, while it wasn't explicit, shows that I have attempted that, too. I've added the Base64Encode function code that is called from here.
Public Sub RESTtest()
Dim restRequest As WinHttp.WinHttpRequest
Set restRequest = New WinHttp.WinHttpRequest
Dim restResult As String
With restRequest
.Open "POST", URL & REQUEST_GET_LOCATIONS, True
.SetRequestHeader "Authorization", "Basic " & SITE_ID & ":" & Base64Encode(API_KEY)
' Note call to Base64Encode() on this line ---------------- ----- ^^^^^^^^^^^^
.Option(WinHttpRequestOption_EnableRedirects) = False
.Send "{""response_type"":""JSON""}"
.WaitForResponse
Debug.Print ".ResponseText: " & .ResponseText
Debug.Print ".Status: " & .Status
Debug.Print ".StatusText: " & .StatusText
Debug.Print ".ResponseBody: " & .ResponseBody
End With
End Sub
Public Function Base64Encode(ByVal inputText As String) As String
Dim xmlDoc As Object
Dim docNode As Variant
Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0")
Set docNode = xmlDoc.createElement("base64")
docNode.DataType = "bin.base64"
docNode.nodeTypedValue = Stream_StringToBinary(inputText)
Base64Encode = docNode.Text
Set docNode = Nothing
Set xmlDoc = Nothing
End Function
Notes:
URL, REQUEST_GET_LOCATIONS, SITE_ID, and API_KEY are constants declared globally in this module for testing purposes. They, too, have all been copy/pasta'd and reviewed by several people for typos.
You may note that there are requests for responses in both XML and JSON - they're both giving me the same response.
I do have a support ticket open with Appt Plus, but I'm hoping I might get a faster response here.
Are there any obvious errors that anyone sees in this code? Are there any suggestions for other methods to attempt to call the API and get results? I've had a suggestion to write a DLL in C# and call that, however, I don't have the time to learn enough C# to make that happen, so switching languages isn't really an option here.
Additional notes:
I tried this using curl in a Powershell session, and it gives me the same result:
PS H:\> curl -method Post -uri "https://ws.appointment-plus.com/Locations/GetLocations?Authorization:Basic=<ID>:<key>&response_type=json"
The result:
StatusCode : 200
StatusDescription : OK
Content : {"resource":"locations",
"action":"getlocations",
"request":"",
"result":"fail",
"count":"0"
,"errors":[
"Web Services authentication failed: invalid Site ID or API ...
RawContent : HTTP/1.1 200 OK
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Date: Mon, 26 Aug 2019 17:28:41 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Set-Cooki...
Forms : {}
Headers : {[Pragma, no-cache], [Cache-Control, no-store, no-cache, must-revalidate, post-check=0,
pre-check=0], [Date, Mon, 26 Aug 2019 17:28:41 GMT], [Expires, Thu, 19 Nov 1981 08:52:00 GMT]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 207
Per the exchanges in the comments, the main issue appears to be how the basic authorization header was being formed.
For future readers, the format for the authorization header is:
.SetRequestHeader "Authorization", "Basic " & Base64Encode(SITE_ID & ":" & API_KEY)
Also, another issue you may run into is related here. Linebreaks are inserted into the Base64 encoded string with the current approach, which won't play nice with most (if not all) APIs. A suggested fix for this would be something like:
Public Function Base64Encode(ByVal inputText As String, Optional removeBlankLines = True) As String
Dim xmlDoc As Object
Dim docNode As Variant
Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0")
Set docNode = xmlDoc.createElement("base64")
docNode.DataType = "bin.base64"
docNode.nodeTypedValue = Stream_StringToBinary(inputText)
Base64Encode = docNode.Text
Set docNode = Nothing
Set xmlDoc = Nothing
'remove blank line characters ASCII --> 10,13,10 + 13
If removeBlankLines Then Base64Encode = Replace(Replace(Replace(Base64Encode, vbCrLf, vbNullString), vbLf, vbNullString), vbCr, vbNullString)
End Function

Custom header with MSXML2.ServerXMLHTTP

I am currently trying to use MSXML2.ServerXMLHTTP to send a POST http request.
I need to add a custom header "Auth" so that my requests get authorized but it doesn't seem to work.
Here is the code for my post function:
Function post(path As String, authToken As String, postData As String) As String
Dim xhr As Object
Dim message As String
On Error GoTo error:
Set xhr = CreateObject("MSXML2.ServerXMLHTTP")
xhr.Open "POST", path, False
xhr.setRequestHeader "Content-Type", "application/json"
xhr.setRequestHeader "Auth", authToken
xhr.send postData
If xhr.Status = 200 Then
message = xhr.responseText
Else
message = xhr.Status & ": " & xhr.statusText
End If
Set xhr = Nothing
post = message
error:
Debug.Print "Error " & Err.Number; ":" & Err.Description
End Function
And I end up with "Error -2147012746 Requested Header was not found" (The message is actually translated since I use a different language on my computer).
However I didn't have this problem with the "Microsoft.XMLHTTP" Object.
Am I doing something wrong ?
Thank you for your time.
Try changing the call to SetRequestHeader to use a string literal. I duplicated the problem when authToken does not have a value set.
Change from this
xhr.setRequestHeader "Auth", authToken
To this
xhr.setRequestHeader "Auth", "testdatahere"