Create SOAP request in AHK? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jaccotjuhhh
Posts: 27
Joined: 16 Mar 2018, 08:45

Create SOAP request in AHK?

01 Feb 2024, 13:55

I am not new to Autohotkey, but i am very new to API's.
i want to learn SOAP API so i can apply that to my work web-applications that support this.

How would one apply this SOAP request to a autohotkey v2 script?
i cannot for the life of me find a good tutorial.

Code: Select all

POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
  <m:GetStockPrice>
    <m:StockName>IBM</m:StockName>
  </m:GetStockPrice>
</soap:Body>

</soap:Envelope>
jaccotjuhhh
Posts: 27
Joined: 16 Mar 2018, 08:45

Re: Create SOAP request in AHK?

01 Feb 2024, 15:55

so far with the resources i managed to find, i got this.

Sadly, my messagebox returns empty

Code: Select all

F1::SendRequest(xml)
^r::reload

xml =
	(
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
  <m:GetStockPrice>
    <m:StockName>IBM</m:StockName>
  </m:GetStockPrice>
</soap:Body>

</soap:Envelope>
	)


SendRequest(xml)
{

	endpoint := "http://www.example.org/stock"
	Response := []
	WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	WebRequest.Open("POST", "http://www.example.org/stock")
	WebRequest.SetRequestHeader("Content-Type", "application/soap+xml; charset=utf-8")
	WebRequest.SetRequestHeader("Content-Length", nnn)
	WebRequest.SetRequestHeader("Host", "www.example.com")

	try
		WebRequest.Send(xml)
	catch
		return
	Response["Text"] := WebRequest.ResponseText

	WebRequest := ""
msgbox %Response%`n

}
mcl
Posts: 359
Joined: 04 May 2018, 16:35

Re: Create SOAP request in AHK?

01 Feb 2024, 16:54

jaccotjuhhh wrote: How would one apply this SOAP request to a autohotkey v2 script?
Your code looks like AHK.v1.
When you pass 'Response' to MsgBox, you get blank string, because it is an array with a key named 'Text'.

I was dealing with SOAP sometime ago (it was not fun), but I used curl and temporary files — sloppy approach, but that worked. As far as I recall, you also need to set HTTP request header 'SOAPAction' and follow WSDL (if you can find one) to the letter.
github://oGDIp - GDI+ wrapper for AHK v1.1
jaccotjuhhh
Posts: 27
Joined: 16 Mar 2018, 08:45

Re: Create SOAP request in AHK?

02 Feb 2024, 19:06

Thanks, stupid error having it an array...
I now have this code, but it is returning FULL HTML code

Code: Select all

F1::SendRequest(xml)
^r::reload

xml =
	(
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">
      <ubiNum>500</ubiNum>
    </NumberToWords>
  </soap:Body>
</soap:Envelope>
	)


SendRequest(xml)
{

	endpoint := "http://www.example.org/stock"
	Response :=
	WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	WebRequest.Open("GET", "https://www.dataaccess.com/webservicesserver/NumberConversion.wso")
	WebRequest.SetRequestHeader("Content-Type", "application/soap+xml; charset=utf-8")
	WebRequest.SetRequestHeader("Host", "www.dataaccess.com")

	try
		WebRequest.Send(xml)
	catch
		return


msgbox % WebRequest.ResponseText


}

What error am i making here?
i am using the example given in https://www.dataaccess.com/webservicesserver/NumberConversion.wso?op=NumberToDollars.
Marium0505
Posts: 42
Joined: 11 May 2020, 20:45

Re: Create SOAP request in AHK?

02 Feb 2024, 23:23

Here's a class you can use:

Code: Select all

class NumberConversion {
	static NumberToWords(InputNumber) => this.GetResult("NumberToWords", "ubiNum", InputNumber)
	static NumberToDollars(InputNumber) => this.GetResult("NumberToDollars", "dNum", InputNumber)

	static GetResult(Operation, InpuType, InputNumber) {
		HTTP_Request := ComObject("WinHttp.WinHttpRequest.5.1")
		URL := Format("https://www.dataaccess.com/webservicesserver/numberconversion.wso/{1}/JSON/debug?{2}={3}", Operation, InpuType, InputNumber)
		HTTP_Request.Open("GET", URL, 0)
		HTTP_Request.SetRequestHeader("Content-Type", "application/json")
		HTTP_Request.Send()
		ResponseText := HTTP_Request.ResponseText, HTTP_Request := ""
		If (InStr(ResponseText, "<html>")) {
			If !(Found := RegExMatch(ResponseText, "im)<br>\R*<b>(.*?)<\/b>", &Match))
				MsgBox("Conversion failed, Response: `n" (Found ? Match[1] : ResponseText), "Conversion failed", "0x1000 0x10 0x0")
			throw Error("Conversion failed", -1, Found ? Match[1] : URL)
		}

		ResponseText := Trim(ResponseText, '" ')
		return ResponseText
	}
}

Example:

Code: Select all

NumberToConvert := "500"
NumberToDollars := NumberConversion.NumberToDollars(NumberToConvert)
NumberToWords := NumberConversion.NumberToWords(NumberToConvert)

MsgBox(Format(
	(Join`n
		'Number To Convert: {1}
	
		NumberConversion.NumberToWords("{1}") 
		=> "{2}"

		NumberConversion.NumberToDollars("{1}") 
		=> "{2}"'
	), NumberToConvert, NumberToWords, NumberToDollars))
mcl
Posts: 359
Joined: 04 May 2018, 16:35

Re: Create SOAP request in AHK?  Topic is solved

03 Feb 2024, 01:15

jaccotjuhhh wrote:
02 Feb 2024, 19:06
What error am i making here?
You switched to use GET instead of POST, for some reason.
Hotkey declarations end auto-execute section, as a result, your 'xml' variable was never defined and request body was empty.

Code: Select all

xml =
(
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">
      <ubiNum>500</ubiNum>
    </NumberToWords>
  </soap:Body>
</soap:Envelope>
)

F1::SendRequest(xml)
^r::reload

SendRequest(xml)
{
	WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	WebRequest.Open("POST", "https://www.dataaccess.com/webservicesserver/NumberConversion.wso")
	WebRequest.SetRequestHeader("Content-Type", "text/xml; charset=utf-8")
	WebRequest.SetRequestHeader("Host", "www.dataaccess.com")

	try
		WebRequest.Send(xml)
	catch
		return

	Msgbox, % WebRequest.ResponseText
	ExitApp
}
github://oGDIp - GDI+ wrapper for AHK v1.1
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Create SOAP request in AHK?

03 Feb 2024, 10:56

Topic moved from "Ask for Help (v2)" since this is v1 code.

@jaccotjuhhh - Please post in Ask for Help (v1) to ask for help with v1 code.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: KilliK, mikeyww and 110 guests