How to pass input parameter to VBA http POST URL - vba

I am new to VBA . I am trying to get some input parameters from InputBox and I want to pass this InputBox element to my post request URL . but I don't know how should I pass the value to my URL?
for eg->
Dim iInput As String
iInput = InputBox("Please enter a number", "Create Invoice Number", "Enter your input text HERE")
Url = "myurl.com/api/v4/projects/3765/ref/master/trigger/pipeline?token=123&variables[check1]=2"
objHTTP.Open "POST", Url, False
here, Instead of 2(in the end) I need to pass this 2 through InputBox by taking userinput. how we should modify the current URL? please let me know.
thanks

You can concatenate two strings with &
Url = "myurl.com/api/v4/projects/3765/ref/master/trigger/pipeline?token=123&variables[check1]=" & iInput

Related

Catch the POST Request Response and the Redirected URL from XMLHTTP request with VBA

I'm trying to catch a Response to a POST Request using XMLHTTP using the code below
Dim XMLPage As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim htmlEle1 As MSHTML.IHTMLElement
Dim htmlEle2 As MSHTML.IHTMLElement
Dim URL As String
Dim elemValue As String
URL = "https://www.informadb.pt/pt/pesquisa/?search=500004064"
XMLPage.Open "GET", URL, False
XMLPage.send
HTMLDoc.body.innerHTML = XMLPage.responseText
For Each htmlEle1 In HTMLDoc.getElementsByTagName("div")
Debug.Print htmlEle1.className
If htmlEle1.className = "styles__SCFileModuleFooter-e6rbca-1 kUUNkj" Then
elemValue = Trim(htmlEle1.innerText)
If InStr(UCase$(elemValue), "CONSTITU") > 0 Then
'Found Value
Exit For
End If
End If
Next htmlEle1
The problem is that I can't find the ClassName "styles__SCFileModuleFooter-e6rbca-1 kUUNkj", because I notice that when I manually insert the value (500004064) in the search box of the URL : https://www.informadb.pt/pt/pesquisa/, the Web Page generates addicinal traffic and turns up at an end point URL : https://www.informadb.pt/pt/pesquisa/empresa/?Duns=453060832, where that className can be found in the Request ResponseText.
My goal is to use the First URL to retrieve the Duns number '453060832', to be able to access the information in the ResponseText of the EndPoint URL. And to catch Duns Number, I need to find a way to get the Endpoint URL, or try to get The POST request response below, and get that value using JSON parser:
{'TotalResults': 1,
'NumberOfPages': 1,
'Results': [{'Duns': '453060832',
'Vat': '500004064',
'Name': 'A PANIFICADORA CENTRAL EBORENSE, S.A.',
'Address': 'BAIRRO DE NOSSA SENHORA DO CARMO,',
'Locality': 'ÉVORA',
'OfficeType': 'HeadOffice',
'FoundIn': None,
'Score': 231.72766,
'PageUrl': '/pt/pesquisa/empresa/?Duns=453060832'}]}
I'm not being able to capture what is really happening using the XMLHTTP Browser request, that seems to be the below steps:
navigate to https://www.informadb.pt/pt/pesquisa/?search=500004064
Webpage generates additional traffic
Amongst that additional traffic is an API POST XHR request which
returns search results as JSON. That request goes to
https://www.informadb.pt/Umbraco/Api/Search/Companies and includes
the 500004064 identifier amongst the arguments within the post body
Based on the API results the browser ends up at the following URI
https://www.informadb.pt/pt/pesquisa/empresa/?Duns=453060832
Can someone help me please, I have to do it using VBA.
Thanks in advance.
A small example how to POST data to your website using VBA, and how to use bare-bones string processing to extract data from the result, as outlined in my comments above.
Function GetVatId(dunsNumber As String) As String
With New MSXML2.XMLHTTP60
.open "POST", "https://www.informadb.pt/Umbraco/Api/Search/Companies", False
.setRequestHeader "Content-Type", "application/json"
.send "{""Page"":0,""PageSize"":5,""SearchTerm"":""" & dunsNumber & """,""Filters"":[{""Key"":""districtFilter"",""Name"":""Distrito"",""Values"":[]},{""Key"":""legalFormFilter"",""Name"":""Forma Jurídica"",""Values"":[]}],""Culture"":""pt""}"
If .status = 200 Then
MsgBox "Response: " & .responseText, vbInformation
GetVatId = Mid(.responseText, InStr(.responseText, """Vat"":""") + 7, 9)
Else
MsgBox "Repsonse status " & .status, vbExclamation
End If
End With
End Function
Usage:
Dim vatId As String
vatId = GetVatId("453060832") ' => "500004064"
For a more robust solution, you should use a JSON parser and -serializer, something like https://github.com/VBA-tools/VBA-JSON.

URL not inserting properly into Outlook message body from string

Dim Urll as string
urll = http://10.0.0.1/Program.exe"
.msgbody = "Click here " & "to open the program"
ends up fragmenting the HTML to where the the URL shows up as a link, but it doesn't use the "Click here" as it's text.
What am I doing wrong?
You probably need to set your message as html
.isBodyHTML = true

VB Web query request

I am trying to send a query request from VB Login form to Website to check if user Email and Password are Valid.
If I am using request string: http://mathsquest.com/api.php?email=testemail&password=testpass
it returns value "1" as "testemail & "testpass" are pre-registered correct values.
How to send the request with user input values stored in Login form variables (EmailInput and PasswordInput)?
Thank you for help
If I correctly understand your question you should build your url request and then make a redirect or an http request:
Dim req_url as string = "http://mathsquest.com/api.php"
req_url &= "?email=" & EmailInput & "&password=" & PasswordInput
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString(req_url)
P.S. As suggested in the comments you should consider hashing/crypting your parameters

XmlHttp Post in Excel VBA not updating website form

I routinely have to search the state of NV for unclaimed property and put the results in an Excel spreadsheet. I am trying to automate the process but I'm limited to using Excel 2010 and VBA. Below is the URL to the site I'm trying to submit a form using xmlhttp.
URL: https://nevadatreasurer.gov/UPSearch/
I created a class to automate submitting forms on other websites but no matter what I enter in the postdata the form is never submitted. Below is my submission, and method to submit the form.
Call to class:
cXML.openWebsite "Post", "https://nevadatreasurer.gov/UPSearch/Index.aspx", _
"ctl04$txtOwner=" & strSearchName
Class method:
Public Sub openWebsite(strOpenMethod As String, strURL As String, _
Optional strPostData As String)
pXmlHttp.Open strOpenMethod, strURL
If strPostData <> "" Then
strPostData = convertSpaceToPlus(strPostData)
pXmlHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
pXmlHttp.send (strPostData)
Else
pXmlHttp.send
End If
'Create DOM html documnet
pHtmlObj.body.innerHTML = pXmlHttp.responseText
End Sub
Each time the responseText is the main website with no updates, as if I submitted no postdata. I'm fairly new to IE automation but can someone provide a reason why this isn't working and a code example that works?
Thanks!
Update: 7/26/13 8:30am PST
Without any changes to my method I was able to submit forms through another website. The state of OR unclaimed property form. It worked perfect!
URL: https://oregonup.us/upweb/up/UP_search.asp
However I ran into the same problem when I tried the state of CA unclaimed property website. No matter what I do, the responseText is always the original search page with no update.
URL: https://scoweb.sco.ca.gov/UCP/Default.aspx
It still does not work with the state of NV on my original post. I am using the proper post data, URL encoded for each website and can see no difference. Any help would be appreciated.
Try below code
Public Sub openWebsite(strOpenMethod As String, strURL As String, Optional strPostData As String)
Dim pXmlHttp As Object
Set pXmlHttp = CreateObject("MSXML2.XMLHTTP")
pXmlHttp.Open strOpenMethod, strURL, False
pXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
pXmlHttp.send (strPostData)
Dim pHtmlObj As Object
Set pHtmlObj = CreateObject("htmlfile")
pHtmlObj.body.innerHTML = pXmlHttp.ResponseText
MsgBox pXmlHttp.ResponseText
End Sub
Sub test()
Dim btnSearch As String, strSearchType As String, strSearchName As String, PostData As String
btnSearch = "Search"
strSearchType = "Owner"
strSearchName = "Santosh"
PostData = "ctl04%24txtOwner=" & strSearchName & "&ctl04%24btnSearch=" & btnSearch & "&ctl04%24rblSearchType=" & strSearchType
openWebsite "POST", "https://nevadatreasurer.gov/UPSearch/Index.aspx", PostData
End Sub
Post Data view using Firebug
URL encode
ResponeText
You should urlencode the characters in this string:
"ctl04$txtOwner=" & strSearchName
Ways to do this are discussed here: SO link, as VBA doesn't have a built-in function for this.
The dollar sign needs to be replaced with %24 and any spaces with %20. If these are the only non-alphanumeric characters in the string they you could take a simple approach, using VBA.Replace() (twice). You are currently replacing spaces with '+' which will usually work, but the dollar-sign may be an issue.

How to put a hyperlink into the email body using vb.net

What im trying to do is add a hyperlink to the body of an email in vb.net. What im getting when i send the email is the link is text. Here is what I doing so far below. Any help would be very much appreciated.
'Accepts two parameters - the username and password for the email client
Dim credentials As New System.Net.NetworkCredential("test#test.net", "test")
smtpClient.Credentials = credentials
Dim body, link As String
link = "http://localhost:" & partUrl & "/test.aspx?autoNum=" & autoNum
body = "Test email." & "<br/><br/>"
body += link
Dim email As New MailMessage
email.From = New MailAddress("test#test.net")
email.Body = body
email.Subject = "test Change/Request Password"
email.To.Add(New MailAddress(toAddress))
smtpClient.Send(email)
You will need to enclose it in a tags.
link = "Click here"
And you need to set
email.IsBodyHtml = true
Try this:
link = "Link"
body = "Test email." & "<br/><br/>"
body += link
The idea (I can't test it now, sorry) is you have to add not the url itself, but the HTML code used to create link.
Remember to set mail body to html with email.IsBodyHtml = True
I believe you need to set
IsBodyHtml = True
Then you can use plain HTML in the body of the e-mail. It's still up to the mail client to display it correctly. I've had a few cases where valid HTML that looked create in my browser was messy in my e-mail.
You haven't identified the body as HTML.
Add:
email.IsBodyHtml = true