Example API Call- Top Reddit post to AutoHotkey subreddit

As we mentioned in our webservice / API webinar most APIs require Authentication.   In this example I noticed the number of records being returned by the API call was not nearly as many as when I did the same call with my browser  (I think this is because I didn’t bother to use my secret/token for oAuth2.)

I decided to use the developer tools in Chrome to see what headers were being sent to reddit.   Once I included the cookie header (data borrowed from my browser) I was returning a ton of data!  I also demonstrated the use of sXML_Pretty function by VxE to re-structure the XML to be “pretty” for humans.

;~  https://www.reddit.com/r/AutoHotkey/top/.xml?count=20

EndPoint:="https://www.reddit.com/r/AutoHotkey/top/.xml"
QueryString:=QueryString_Builder({"count":"20"})
;~  MsgBox % QueryString
;***********API call*******************
http:= ComObjCreate("WinHttp.WinHttpRequest.5.1")
http.Open("GET",EndPoint . QueryString)
HTTP.SetRequestHeader("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
HTTP.SetRequestHeader("Accept-Encoding", "gzip,deflate,sdch") ;sdch is Google Shared Dictionary Compression for HTTP
HTTP.SetRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.40 Safari/537.36")
HTTP.SetRequestHeader("Cookie", "reddit_session=38800806%2C2016-09-09T03%3A57%3A44%2Ca42aa9c84f0e5593d7ab3a5c9eadda53d524f367; secure_session=1; _recent_srs=t5_2rodl%2Ct5_3fih6; loid=0000000000000n3mw6.2.1429884359891.Z0FBQUFBQlpSblVqZG9ZTC1TUXlRa09oOWpaSk15THVfX3hUNzd0VGRtTkhYQlY4MURRQU95OXJUT0xocmVNdXBnak9xeUF0NERvYkFENnk5TFRFYmRlS055aThrV3Qwa05tN2k2a2lZS3pXTTRjcFFFOGk5U0ZlTWZMN1phd3pBMDFQQjF0RHpDN20; loid_old=IMXIEZOtTblUWbNr5Z; edgebucket=RCDVR90bTmS8Z00rQO; joetazz_recentclicks2=t3_2c2ktw%2C; joetazz_recent_srs=t5_2qizd%2Ct5_2rodl%2Ct5_3fih6%2Ct5_34em3; pc=fj; session_tracker=K0KAEeutMKNQ4Qw5Sw.0.1498341387189.Z0FBQUFBQlpUdUFMUjd6cllHNUM0YWdBN2lUbWNCVFVyT2d6d2xFVmQyby1PWXl5b3pUMXhuTTQyU3YzRGVPNXpxeHVoSXA4MVZUU1d2R0NsQ0tUeGo5N2pmcVFvdk9xT1B1RXg1TjlYRTl5Rl80T05BV0l6elNfOEFveExabHhlY3FXR3I4S2lGZHI; initref=google.com")

http.Send()
;~  SciTE_Output(http.Responsetext) ;Text,Clear=1,LineBreak=1,Exit=0
SciTE_Output(sXML_Pretty(http.Responsetext,"   ")) ;Text,Clear=1,LineBreak=1,Exit=0

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

Here is the video where I document the Example API Call and show how to use the Chrome Developer tools

Comments are closed.