Post to RESTful web service - xmlhttprequest

I have this code but i cant seem to get around how to use same code with body parameters. I have to add two body parameters of number = 123456 and first_name = james. Please help update the following code to allow that and work within VB6.
Dim sUrl As String
Dim response As String
Dim xmlhttp
Set sUrl = "http://my.domain.com/service/operation/param"
Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "POST", sURL, False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send()
Dim response As String = xmlhttp.responseText
Set xmlhttp = Nothing

Related

"LOADING" SOAP response to DOMDocument

After some help from stackoverflow experts I have been able to successfully retrieve my response using SOAP. The below piece is how I received and stored the data. This of course is not all the code. I just included this to show how I later reference the xml.
With xmlhtp
webserviceSOAPActionNameSpace = "http://example.com/webservices/"
.Open "POST", sUrl, False
.setRequestHeader "POST", "https://onesite.example.com/webservices/stuff.asmx HTTP/1.1"
.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
.setRequestHeader "Content-Length", 100
.setRequestHeader "SOAPAction", webserviceSOAPActionNameSpace & "RetrieveData"
.send sEnv
sResult = xmlhtp.statusText
responseText = xmlhtp.responseText
ActiveSheet.Cells(1, 1).Value = .responseText
End With
Debug.Print responseText
Now I am having trouble parsing that out. This seems like it should be pretty simple but I get an error indicating that the responseText I receive above is not "loading" to xmlDOC. The following is at the beginning of the sub:
Dim xmlhtp As New MSXML2.XMLHTTP
Dim xmlDoc As New DOMDocument
Dim XDoc As Object
After the With End (shown above) my code looks like this:
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.Load (xmlhtp.responseText)
Set lists = XDoc.DocumentElement
Set getFirstChild = lists.FirstChild
Debug.Print getFirstChild.XML
Debug.Print getFirstChild.Text
On the line
Set getFirstChild = lists.FirstChild
I recieve the following error
Object variable or With block variable not set
When I look at the Local Variable window in VBA I can clearly see that nothing was assigned to xmlDoc. So I assume my problem is in XDoc.Load Line.
Any direction would be appreciated.
use XDoc.LoadXML (xmlhtp.responseText) instead of XDoc.Load (xmlhtp.responseText)

VBA RESTful API GET Method issue

Hi I'm calling an API and using the winhttp request and the GET method. I'm passing a json style parameter in the send method but it just can't get accepted. I end up with the error message:
{"code":"INS03","description":"Event ID required","requestId":"_181603230829162847306080","data":{},"validationErrors":null}
Which seems weird because I am indeed passing the event id parameter, as follows:
inventory_URL = "https://api.stubhub.com/search/inventory/v1"
Dim oRequest As WinHttp.WinHttpRequest
Dim sResult As String
Set oRequest = New WinHttp.WinHttpRequest
With oRequest
.Open "GET", inventory_URL, True
.setRequestHeader "Authorization", "Bearer " & access_token
.setRequestHeader "Accept", "application/json"
.setRequestHeader "Accept-Encoding", "application/json"
.send ("{""eventid"":""9445148""}")
.waitForResponse
sResult = .responseText
Debug.Print sResult
sResult = oRequest.Status
Debug.Print sResult
End With
Is there any issue with my code?
Thank you in advance,
Vadim
For GET request query string should be composed.
Your data can't be passed in Send but in query string:
.Open "GET", inventory_URL & "?eventid=9445148", True
Check GET vs POST.

Add an attachment using: api/2/issue/{issueIdOrKey}/attachments

I have successfully been able to create a Jira Issue, now want to to add an attachment using: api/2/issue/{issueIdOrKey}/attachments
My following VB code wont work, any suggestions please as this is new to me, thanks. The error I get is: HTTP Status 500 - Error parsing Content-Type
Sub JIRA_PostAttachment()
Dim pHtml As String
Dim oHttp As Object
Dim strResponse As String
pHtml = "https://jira.ae.sda.corp.test.com/rest/api/2/issue/IS-163/attachments"
sVar = """file="": ""C:\Users\c776469\FORM.msg"""
Set oHttp = CreateObject("Microsoft.XMLHTTP")
Call oHttp.Open("POST", pHtml, False)
'oHttp.SetRequestHeader "Content-Type", "application/json"
oHttp.SetRequestHeader "Content-Type", "multipart/form-data;Charset=UTF-8; boundary="
'oHttp.SetRequestHeader "Accept", "application/json"
oHttp.SetRequestHeader "X-Atlassian-Token", "no-check"
oHttp.SetRequestHeader "Authorization", "Basic Yzc4NjQ3OTpHaWxpdDIwMTY/"
Call oHttp.Send(sVar)
strResponse = oHttp.ResponseText
MsgBox strResponse
Set oHttp = Nothing
End Sub
Please look at this link:
https://answers.atlassian.com/questions/39127148/answers/39127815/comments/39130956
It has a VBA example that works (also a C# example)

MSXML2.ServerXMLHTTP - can't access website

For instance, kat.cr is one of the few websites I cannot access.
In the response, I get 2 symbols instead of the actual webpage:
Here's the VBA code I'm using:
url = "https://kat.cr"
Set xmlHTTP = CreateObject("MSXML2.serverXMLHTTP")
xmlHTTP.Open "GET", url, False
xmlHTTP.setRequestHeader "Accept-Language", "en-US,en;q=0.8"
xmlHTTP.setRequestHeader "Content-Type", "text/xml"
xmlHTTP.Send
Set html = CreateObject("htmlfile")
response = xmlHTTP.responseText
Is the website actually denying me access or am I doing something wrong?
Use CreateObject("MSXML.XMLHTTP") for client applications. ServerXMLHttp is meant for server applications. See this article for more information on the two.
Set xmlHTTP = CreateObject("MSXML2.XMLHTTP")
xmlHTTP.Open "GET", "https://kat.cr", False
xmlHTTP.Send
Debug.Print xmlHTTP.responseText
The website is not denying you access, it's your code I am afraid. Below is a quick example to get the HTML you want from the page you visit.
Note: This was just a quick type-o, but to help get you in the right direction.
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "https://kat.cr"
Set html = ie.document
'this is the inner html -html.DocumentElement.innerHTML
Set ie = Nothing
Edit - This might be a better solution for you
Set xmlHTTP = New MSXML2.XMLHTTP
xmlHTTP.Open "GET", "https://kat.cr", False
xmlHTTP.send
Dim doc As Object
Set doc = CreateObject("htmlfile")
doc.body.innerHTML = xmlHTTP.responseText
debug.print doc.body.innerHTML

Send data using msxml2.xmlhttp.3.0 to web to select Datepicker in EXCEL VBA

The following is the HTML of part of a web page.
"input name="ctl00$ctl00$AllContent$ContentMain$ucMktStatCtl$txtDate" type="text"
id="ctl00_ctl00_AllContent_ContentMain_ucMktStatCtl_txtDate"
onkeypress="javascript:return fnTrapKD(event, document.getElementById('ctl00_ctl00_AllContent_ContentMain_ucMktStatCtl_butReport'))"
value="02/24/2006" class="hasDatepicker">
I tried to use the following code to access the data.
Dim strPostData As String: strPostData = "ctl00$ctl00$AllContent$ContentMain$ucMktStatCtl$txtDate=02/24/2006"
Dim xmlhttp: Set xmlhttp = CreateObject("msxml2.xmlhttp.3.0")
xmlhttp.Open "POST", "http://www.cboe.com/data/mktstat2.aspx#VIX", False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send (strPostData)
I am getting responsetext with 404 - File or directory not found. But the site does accept the input in a browser.
The mozilla firefox addon firebug helps to analyse the http request.
Post tab shows the parameters which are sent.
The URL should be http://www.cboe.com/data/mktstat2.aspx
Sub test()
Dim strPostData As String
strPostData = "ctl00$ctl00$AllContent$ContentMain$ucMktStatCtl$butReport=Get Report&ctl00$ctl00$AllContent$ContentMain$ucMktStatCtl$ddlNav=&ctl00$ctl00$AllContent$ContentMain$ucMktStatCtl$txtDate=05/31/2013&ctl00$ctl00$AllContent$ucHeader$CBOEHeaderSearchBox$txtHeaderSearch=Search&ctl00$ctl00$AllContent$ucHeader$ucCBOEHeaderQuoteBox$txtHeaderQuote=Quote"
Dim xmlhttp As Object
Set xmlhttp = CreateObject("msxml2.xmlhttp")
xmlhttp.Open "POST", "http://www.cboe.com/data/mktstat2.aspx", False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send (strPostData)
MsgBox xmlhttp.responseText
End Sub