How to Return Specific Data from WSDL GET function in VBA - vba

Basically what I'm trying to do is create an excel sheet that can query an item number and return it from a wsdl file on our web service, but no matter what code I try, it only returns the wsdl file itself.
strRequest = "http://webserver.com/WebServices/E1/Test.wsdl"
With objXmlHttp
.Open "POST", strRequest, False
.Open "GET", "http://webserver.com/WebServices/E1/Test.wsdl", False
.setRequestHeader "Content-Type", "application/xml"
.setRequestHeader "Authorization", "Basic username:password"
End With
I've read that you can request information by adding ?wsdl=(query request) to the end of the GET url, but again, I only get wsdl. What is it I'm missing or not understanding? I've been googling for days and I can't find any helpful information

Related

Create Sharepoint folder from VBA (access 2016)

I have an access 2016 app that currently creates a folder on a shared drive related to the DB record the user is working on.
We are migrating to SP online and I need to re-create something similar
I can list the files and folders currently in a directory using the API but when I try and create a new folder I am getting an error
"{"error":{"code":"-2130575251, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."}}}"
The only thing I can see I am doing differently is I am not including a Authorization: "Bearer " + accessToken header in the post.
I haven't been including it in the gets for the Files list and it is working using the cached credentials from IE.
Getting the file and folder like this is working
'create XML HTTP object
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
With objXMLHTTP
'open connection to site
.Open "GET", url, False
'.send
.setRequestHeader "Content-Type", "application/json; charset=UTF-8"
.setRequestHeader "Accept", "application/json"
.send
Do Until .ReadyState = 4: DoEvents: Loop
sJSONString = .responseText
End With
Dim Json As Object
Set Json = JsonConverter.ParseJson(sJSONString)
Debug.Print JsonConverter.ConvertToJson(Json, Whitespace:=2)
Dim value As Dictionary
For Each value In Json("value")
Debug.Print "> " + value("Name")
Next value
However trying to create a folder fails
strPostData = "{ '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/Shared Documents/Folder1'}"
'Set objXMLHTTP = New MSXML2.XMLHTTP
With objXMLHTTP
.Open "POST", url, False
.setRequestHeader "accept", "application/json;odata=verbose"
.setRequestHeader "Content-Type", "application/json;odata=verbose"
.send strPostData
strResponse = .responseText
End With
Can anyone advise if what I am trying is possible and if so what I am doing is wrong?
Sorry, almost immediately I found and answer!
Needed to add
.setRequestHeader "X-RequestDigest", digest
.setRequestHeader "Content-Length", Len(strPostData)
and it all started working!
Thanks anyway, hope this helps someone else

Token authentication for CURL in EXCEL VBA

A client requested that I put together a tool to take a company domain name and somehow come up with the account name. I found an API that I can work with, but I'm not as familiar with doing this in VBA.
Here is the CURL procedure that the site documentation gives to push through a Domain Name, and receive the company name as a response:
curl 'https://company.clearbit.com/v2/companies/find?domain=segment.com' \
-u sk_b3b05a8924c4f3df86248b4e38421cfa:
After several attempts of receiving the "Please authenticate your request", I've finally gotten to the point where I am receiving an error from the API, I think indicating that I am now correctly calling the API in some way.
Here is my current code. Any idea why I may be receiving this error?
{"error":{"type":"api_error","message":"Sorry, something went wrong. We have been notified."}}
Public Function CallRestAPI2(strUrl1 As String)
TargetURL = "https://company.clearbit.com/v2/companies/find?domain=toplinegroup.com"
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
'HTTPReq.Option(4) = 13056 '
HTTPReq.Open "GET", TargetURL, False
HTTPReq.SetRequestHeader "Content-Type", "application/json"
HTTPReq.SetRequestHeader "Accept", "application/json"
HTTPReq.Send "-u sk_b3b05a8924c4f3df86248b4e38421cfa"
Debug.Print HTTPReq.responseText
End Function
Any idea what I am doing wrong?
Figured this out after more reading in the documentation and it was due to not passing the Authentication header the correct way. Here is my code:
Public Function CallRestAPI3(strUrl1 As String)
Dim xmlhttp As New MSXML2.XMLHTTP60, myurl As String
myurl = "https://company.clearbit.com/v2/companies/find?domain=toplinegroup.com"
xmlhttp.Open "GET", myurl, False
xmlhttp.setRequestHeader "Authorization", "Bearer " + "sk_b3b05a8924c4f3df86248b4e38421cfa"
xmlhttp.Send
Debug.Print (xmlhttp.responseText)
End Function

VBA HTTP response text does not support Japanese

I have a VBA program that sends REST API request and get response.
I use WinHttp.WinHttpRequest to send request but the response text does not support Japanese.
It means if REST server returns result with Japanese, HTTP response text in VBA will be strange characters (e.g. ?aa??a).
I tried to SetRequestHeader with Accept-Language = ja-JP, Charset = UTF-8...but it does not work.
Please help me to resolve this problem.
My code here:
Dim oHttp As WinHttp.WinHttpRequest
Set oHttp = New WinHttp.WinHttpRequest
oHttp.Open "GET", "url", False
oHttp.SetRequestHeader "Accept", "application/json"
oHttp.SetRequestHeader "Content-Type", "application/json"
oHttp.SetRequestHeader "Authorization", "Bearer <token>")
oHttp.Send (sData)
Debug.Print oHttp.ResponseText
Thanks
I found a solution for that: use ServerXMLHTTP instead of WinHttpRequest.

POST not working, while GET works fine (xmlhttp)

I'm writing an Excel macro to communicate with my API, and it seems I cannot send an request via POST, only GET is working.
My code:
Private Sub button1_Click()
Dim xmlhttp As New MSXML2.xmlhttp, myurl As String
myurl = "https://{my_url}"
xmlhttp.Open "POST", myurl, False
xmlhttp.setRequestHeader "Content-Type", "application/xml"
xmlhttp.send
MsgBox (xmlhttp.responseText)
End Sub
Macro run returns:
No resource method found for POST, return 405 with Allow header
When I modify the code, changing POST to GET:
xmlhttp.Open "GET", myurl, False
It works fine, I get the response. How can I force excel to cooperate with POST method? Since my API for more advanced communication requires POST?
Upon further investigation there was nothing wrong with my VBA code. The response I got, was sent from my API. Thank you for comments, they helped me to resolve the issue by including XML body into xmlhttp.send command.

Excel VBA MSXML2 Post response body empty runtime error -2147467260

I would like to post data to Jira using this method :
Private IEService As New MSXML2.XMLHTTP60
Public Function httpPOST(baseUrl As String, url As String, jsonInput As String, auth As String) As String
Dim accept As String
accept = "application/json"
With IEService
.Open "POST", baseUrl & url, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", accept
.setRequestHeader "Authorization", "Basic " & auth
.setRequestHeader "Origin", baseUrl
.send jsonInput
httpPOST = .responseText
'MsgBox (.responseText)
End With
End Function
Data is fine and the POST is executed on the back end side. But Jira in this instance returns an empty response body.
When this happens VBA crashes on .send jsonInput with error : runtime error -2147467260
Seems like MSXML2 does not handle empty responses. But I haven't found anyone facing same issue searching on the web.
Any ideas what I am doing wrong or how to fix it ?
Thanks