xmlHTTPRequest vs. WinHTTPRequest- What killer advantages does the xmlRequest have?

xmlHTTPRequest vs. WinHTTPRequestIn this video I walk through both the xmlHTTPRequest and the WinHTTPRequest and I compare the xmlHTTPRequest vs. WinHTTPRequest.  As I mention in the video, Jackie Sztuk and I have a great  AutoHotkey webinar on Intro to API calls.  I also have several examples on my API page.

I also mention using Fiddler to monitor the browser traffic and grab your cookies / headers.

xmlHTTPRequest vs. WinHTTPRequest tutorial


Here’s the syntax I used for the WinHTTPRequest example

WinHTTPRequest syntax

And here’s the corresponding two XML API calls I demonstrated in the video

xmlHTTPRequest example using Msxml2.XMLHTTP COM object

Here’s the example connecting to the IE page and sending the API request with the xmlHTTPRequest()

How I used Google Translate to save the client both time and money

In this session of “what I’ve automated with AutoHotkey” I explain how I saved my client a ton of time and money by using the Google Translate API.

How I used Google Translate to save the client both time and money

Below is the code I used in the code to Google Translate to save the client both time and money.  Make sure you put my Excel function library and Maestrith’s Notify function in your library.

#SingleInstance,Force
Global API_Key,ID,All,From,To,XL,EndPoint,LangObj
Global Column_to_Translate:="A", Column_To_Put_Translations:="B"
;**************************************
EndPoint:="https://www.googleapis.com/language/translate/v2" ; ?parameters
IniRead, API_Key,Auth.ini,API, Key ;read current token (You'll need to get your own)

XL:=XL_Handle(1)
if ! IsObject(Xl) { ;Joe added 9/22 to make sure Excel is loaded before running
  MsgBox % "Please Start Excel then click this box"
  Reload
}

LangObj:={"Chinese Trad":"zh-CN","Chinese Simp":"zh-TW",Italian:"it",Czech:"cs",French:"fr",German:"de",Hebrew:"he",Indonesian:"id",Japanese:"ja",Korean:"ko",Malay:"ms",Polish:"pl",Portuguese:"pt",Russian:"ru",Spanish:"es",Thai:"th",Turkish:"tr",Vietnamese:"vi",English:"en"}
for a in LangObj ;Build Language object for ComboBox
  LangList.=a "|"

All:=[]
gui,+hwndMain
ID:="ahk_id" Main
gui,Add,ListView,w800 h300,Text
gui,Add,ComboBox,vFrom,% Trim(LangList,"|") ;~ https://cloud.google.com/translate/docs/languages
gui,Add,ComboBox,x+m vTo,en||it|es|la ;Languages to translate into
gui,Add,Text,x+60, Column to be translated 
gui,Add,ComboBox,x+m w50 vColumn_to_Translate,A||B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z  ;Store which column to translate
gui,Add,Text,x+60, Destination for translations
gui,Add,ComboBox,x+m w50 vColumn_To_Put_Translations,A|B||C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z ;Store where to put the translations
gui,Add,Button,xm gLang,Discover Language 
gui,Add,Button,x+25 gTranslate,Translate
gui,Add,Button,xm gRefresh,Refresh
Refresh()
gui,Show
return

Refresh(){
  Rows:=XL.ActiveSheet.UsedRange.Rows.Count,LV_Delete(),All:=[]
  while(A_Index<=Rows){
    if(Value:=(Obj:=XL.Range(Column_to_Translate . A_Index)).Value)
      All.Push(Obj),LV_Add("",Value)
    if(All.MaxIndex()=15)
      Break
  }LV_Modify(1,"Select Vis Focus")
}
Translate(){
  Gui,Submit,NoHide
  From:=LangObj[From]?LangObj[From]:From
  if(!From||!To)
    return m("Both Language values must be set")
  XL.Columns(Column_To_Put_Translations).ColumnWidth:=100
  Rows:=XL.ActiveSheet.UsedRange.Rows.Count
  Blank:=0,BlankInput:=0
  while(A_Index<=Rows){
    if(!(Obj:=XL.Range(Column_To_Put_Translations . A_Index)).Value){
      Blank:=0
      t("Processing")
      if(Value:=XL.Range(Column_to_Translate . A_Index).Value){
        QueryString:=QueryString_Builder({"source":From,"target":To,"q":Value,"key":API_Key})
        RegExMatch(Val:=API_Call(EndPoint,queryString),"OUi)\x22translatedText\x22:\s+\x22(.*)\x22",Found)
        Text:=Found.1
        while(RegExMatch(Text,"OU)(\&#(\d+);)",Found))
          Text:=RegExReplace(Text,"\Q" Found.1 "\E",Chr(Found.2))
        while(RegExMatch(Text,"OU)(\&quot;)",Found))
          Text:=RegExReplace(Text,"\Q&quot;\E",Chr(34))
        Obj.Value:=Text
      }
    }else{
      Blank++
      m(Blank)
      if(Blank=5)
        Break
    }
  }
  t(),Notify(60).AddWindow("Complete",{size:14,Animate:"Center",ShowDelay:1000,Icon:161,IconSize:15,Title:"Translations",TitleSize:14,Color:"0x00FF00",TitleColor:"0xFF0000",time:6000})
}
GuiEscape:
GuiClose:
ExitApp
return
;********************Language detection***********************************
Lang(){
  Lang:=API_Call("https://translation.googleapis.com/language/translate/v2/detect",QueryString_Builder({q:All[LV_GetNext()].Value,key:API_Key}))
  RegExMatch(Lang,"OUi)\x22language\x22:\s+\x22(.*)\x22",Found)
  for a,b in LangObj
    if(b=Found.1)
      Language:=a
  ControlSetText,ComboBox1,% Language?Language:Found.1,%ID%
}
;********************API Call***********************************
API_Call(EndPoint,queryString){
  static WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
  WebRequest.Open("GET", EndPoint . queryString)
  WebRequest.SetRequestHeader("Accept", "text/xml;charset=UTF-8")
  WebRequest.Send()
  return Response:=WebRequest.ResponseText
}
;********************URI Encode charachters***********************************
UriEncode(Uri, full = 0){
  oSC := ComObjCreate("ScriptControl")
  oSC.Language := "JScript"
  Script := "var Encoded = encodeURIComponent(""" . Uri . """)"
  oSC.ExecuteStatement(Script)
  encoded := oSC.Eval("Encoded")
  Return encoded
}

 

AutoHotkey Web service API syntax writer- Automate writing your WinHTTP Requests with my syntax writer

AutoHotkey API syntax writer

In this video I walk through how to use my AutoHotkey API Syntax Writer.  If you’re new to APIs, be sure to check out our Webinar on API / Web Services or this page for a lot of examples of connecting to API services.

Please remember that, after launching the script, you’ll need to hold down the control key and left mouse-click to activate the menu options.AutoHotkey Merchandise-White Stress ball

Here is the video walking through how to use the tool.

 

Example Webservice / API call- 4 GoogleMaps API services

In the following video I work through connecting for four separate APIs which are all under GoogleMaps.  It is very easy to connect to them with AutoHotkey.  Simply get your own token and then adapt the below code to your needs!

Video reviewing the four GoogleMaps APIs.

AutoHotkey Merchandise-White Stress ball

GoogleMaps Directions API

;~  https://developers.google.com/maps/documentation/directions/
IniRead, API_Key,Auth.ini,API,Key ;read current token
queryString:=QueryString_Builder({"origin":"75 9th Ave New York, NY","destination":"MetLife Stadium 1 MetLife Stadium Dr East Rutherford, NJ 07073","key":API_Key})

Endpoint:="https://maps.googleapis.com/maps/api/directions/" . "xml" ;xml or json

;***********API call*******************
HTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HTTP.Open("GET", Endpoint . queryString) ; "GET" "POST" ; be sure to uppercase
HTTP.SetRequestHeader("Content-Type","application/x-www-form-urlencoded")
HTTP.Send()
Response:=HTTP.ResponseText
SciTE_Output(Response)
;~  SciTE_Output(sXML_Pretty(Response,"   ")) ;Text,Clear=1,LineBreak=1,Exit=0

;***********query string builder*******************
QueryString_Builder(kvp){
for key, value in kvp
  queryString.=((A_Index="1")?(url "?"):("&")) key "=" value
return queryString
}

GoogleMaps GeoCode API

;~  https://developers.google.com/maps/documentation/geocoding/start
IniRead, API_Key,Auth.ini,API,Key ;read current token

;~  queryString:=QueryString_Builder({"address":"836 Kilbridge Lane, Coppell, TX","key":API_Key})
queryString:=QueryString_Builder({"address":"1600 Amphitheatre Parkway, Mountain View, CA","key":API_Key})

Endpoint:="https://maps.googleapis.com/maps/api/geocode/" . "xml" ;xml or json

;***********API call*******************
HTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HTTP.Open("GET", Endpoint . queryString) ; "GET" "POST" ; be sure to uppercase
HTTP.SetRequestHeader("Content-Type","application/x-www-form-urlencoded")
HTTP.Send()
Response:=HTTP.ResponseText
SciTE_Output(sXML_Pretty(Response,"   ")) ;Text,Clear=1,LineBreak=1,Exit=0

;***********query string builder*******************
QueryString_Builder(kvp){
for key, value in kvp
  queryString.=((A_Index="1")?(url "?"):("&")) key "=" value
return queryString
}

GoogleMaps Places

IniRead, API_Key,Auth.ini,API, Key ;read current token
Endpoint:="https://maps.googleapis.com/maps/api/place/nearbysearch/xml" ; json or xml
queryString:=QueryString_Builder({"location":"32.9939970,-96.9947992","radius":"1000","types":"food","key":API_Key})

API_Call(Endpoint,queryString)
;******************************
API_Call(Endpoint,queryString){
HTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HTTP.Open("GET", Endpoint . queryString )
HTTP.Send()
Response:=HTTP.ResponseText
SciTE_Output(Response) ;Text,Clear=1,LineBreak=1,Exit=0
}

;***********query string builder*******************
QueryString_Builder(kvp){
for key, value in kvp
  queryString.=((A_Index="1")?(url "?"):("&")) key "=" value
return queryString
}

GoogleMaps TimeZone

IniRead, API_Key,Auth.ini,API,Key ;read current token

 ;~  The following example requests the time zone data for Washington, DC, USA, on March 15, 2016, in JSON format:
queryString:=QueryString_Builder({"location":"38.908133,-77.047119","timestamp":"1458000000","key":API_Key}) ;location is lat/long tuple

Endpoint:="https://maps.googleapis.com/maps/api/timezone/" . "json" ;xml or json

;***********API call*******************
HTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HTTP.Open("GET", Endpoint . queryString) ; "GET" "POST" ; be sure to uppercase
HTTP.SetRequestHeader("Content-Type","application/x-www-form-urlencoded")
HTTP.Send()
Response:=HTTP.ResponseText
SciTE_Output(Response) ;Text,Clear=1,LineBreak=1,Exit=0