Jump to content
Ward

Request UDF - The simplest way to make HTTP request

Recommended Posts

Ward

Like Request for node.js or Requests for python, Request UDF is a powerful and easy-to-use HTTP client for AutoIt. 

It is based on my Curl.au3 and Json.au3 (not included). 

The Features:

  • Request target can be a string (single url), an array (urls), an object (single url + options or url array + options), an object array (urls + different options), or a json string (decode to array or object automatically), etc.
  • Easy to setup post data, cookies, agent, refer, timeout, proxy, etc.
  • Easy to setup the default options before a series of requests.
  • Use BinaryToString() to decode the returned data according to content type automatically. 
  • Use curl multi interface by default. It means all requests will transfer simultaneously and won't block the GUI event.
  • Use callback function to recieve the data and information for every url before main function finished.
  • Supports https and ftp protocols.

Example:

#Include "Json.au3"
#Include "Curl.au3"
#Include "Request.au3"

RequestDefault('{refer: "http://www.autoitscript.com", agent: "AutoIt/Request", callback: "MyCallback"}')

Local $Data = Request("http://httpbin.org/get")
ConsoleWrite($Data & @LF)

Request('["http://www.google.com", "http://wikipedia.org"]')
Request('{url: "http://www.google.com", agent: "AutoIt/Request"}')
Request('{url: ["http://www.google.com", "http://wikipedia.org"], agent: "AutoIt/Request"}')
Local $Array[] = ["http://www.google.com", "http://wikipedia.org"]
Request($Array)
Request("http://httpbin.org/post", "key=Post%20can%20be%20the%20raw%20data")
Request("http://httpbin.org/post", '{key: "Post can be a json object string"}')
Request('{url: "http://httpbin.org/post", post:{key: "Post can be set as second paramemtr or \"post\" option"}}')
Local $Obj
Json_Put($Obj, ".key", "Post can be an object")
Local $Json = Request("http://httpbin.org/post", $Obj)

Func MyCallback($Obj)
    Local $Data = Json_ObjGet($Obj, "Data")
    Local $EffectiveUrl = Json_ObjGet($Obj, "EffectiveUrl")
    ConsoleWrite("Callback: " & $EffectiveUrl & " download size: " & StringLen($Data) & @LF)
EndFunc

More examples are in the archive.

Request.zip

Edited by Ward
  • Like 2

新版 _ArrayAdd 的白痴作者,不管是誰,去死一死好了

 

Share this post


Link to post
Share on other sites
water

Added to the UDF list in the wiki :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
qwert

@Ward: which is the right BinaryCall.au3?  (It's in two of the zips.)

I went with the 2015 version and got some undefined fcns.

Also, I got the error below.  Typo?

 

Error.PNG

Edited by qwert

Share this post


Link to post
Share on other sites
water

Shouldn't there be a space character in the last line before "Then $Callback = Execute($Callback)"


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
qwert

Yes, adding a space takes care of that.  But has anyone been able to run the example?  If so, with which BinaryCall.au3?

My attempt is stopped by two missing MemoryModule functions.

 

Share this post


Link to post
Share on other sites
argumentum

qwert, change this part in BinaryCall

If $HasMemoryDll Then
                Local $Module = Execute('__MemoryModule_ModuleRecord("get", Null, $DllName)')
                If $Module Then
                    Local $MemoryGetProcAddress = Execute('__MemoryModule_RuntimeLoader("MemoryGetProcAddress")')
                    If $MemoryGetProcAddress Then $Proc = DllCallAddress("ptr:cdecl", $MemoryGetProcAddress, "ptr", $Module, "str", $ProcName)[0]
                    If Not $Proc Then Return SetError(2, _BinaryCall_LastError("MemoryGetProcAddress failed on " & $ProcName), False)
                EndIf
            EndIf

 

Share this post


Link to post
Share on other sites
qwert

That took care of it.  Using the above example, I got the result shown below.

The actions are logged ... but are the downloads actually stored?

 

 

Result.thumb.PNG.1cd635b191b05c8e6c269af

 

Edited by qwert

Share this post


Link to post
Share on other sites
argumentum

That took care of it.  Using the above example, I got the result shown below.

I don't see those returns in Request_Example.au3

Share this post


Link to post
Share on other sites
qwert

What, then, would be an appropriate write statement?  I guess I'm asking where is the content for a download and what format is it?

Sorry to be so persistent, but this is all brand new to me and I'm trying to get a handle on its use.

Share this post


Link to post
Share on other sites
argumentum

What, then, would be an appropriate write statement?  I guess I'm asking where is the content for a download and what format is it?

Sorry to be so persistent, but this is all brand new to me and I'm trying to get a handle on its use.

​is plain text. reading the code you could do:

Func MyCallback($Obj)
    Local $Data = Json_ObjGet($Obj, "Data")
    Local $EffectiveUrl = Json_ObjGet($Obj, "EffectiveUrl")
    ConsoleWrite('--- '&$Data&' ---'&@LF) ; <--- to see the downloaded data
    ConsoleWrite("Callback: " & $EffectiveUrl & " download size: " & StringLen($Data) & @LF)
EndFunc

Unfortunately not everything we find here is for just copy, paste and shine. Read the code. After 700+ posts you should know how.

Ward, thanks for sharing, great code.

Edited by argumentum

Share this post


Link to post
Share on other sites
qwert
$Data = Json_ObjGet($Obj, "Data")

That was the call I needed to see.

Thanks for your help.

  • Like 1

Share this post


Link to post
Share on other sites
topten

Thank you very much for such a great UDF. I am just trying to use it

#Include "Json.au3"
#Include "Curl.au3"
#Include "Request.au3"

Local $Backup = RequestDefault('{agent: "Autoit/Request", cookiefile: "cookie.txt", cookiejar: "cookie.txt", }')
Local $Data = Request('http://www.w3schools.com/html/html_forms.asp')
Local $Post
Json_Put($Post, ".firstname", "intro")
Json_Put($Post, ".lastname", "intro")
    Local $Obj = Request('{url: "http://www.w3schools.com/html/action_page.php", returnobj: true}', $Post)
FileWrite (@DesktopDir & "\3.txt", $Obj)

What am I doing wrong in this code? It returns nothing

Great thanx in advance!

Share this post


Link to post
Share on other sites
recmund

 I needed PUT request for my REST application, so I quickly added CUSTOM_REQUEST support.

See cURL MAN page - -X, --request <command>

 

11c11
< ;   Request($Request, $Post = Null)
---
> ;   Request($Request, $Post = Null, $CustomRequest = Null)
108c108
< Func Request($Request, $Post = Null)
---
> Func Request($Request, $Post = Null, $CustomRequest = Null)
179a180,181
>     If Not IsKeyword($CustomRequest) Then Json_ObjPut($OptList, "customrequest", $CustomRequest)
> 
311a314
>     If Json_ObjExists($Obj, "customrequest") Then Curl_Easy_Setopt($Curl, $CURLOPT_CUSTOMREQUEST, Json_ObjGet($Obj, "customrequest"))
320a324
> 

 PATCH 

Request.au3

Edited by recmund

Share this post


Link to post
Share on other sites
fairylovehn127

 I needed PUT request for my REST application, so I quickly added CUSTOM_REQUEST support.

See cURL MAN page - -X, --request <command>

 

11c11
< ;   Request($Request, $Post = Null)
---
> ;   Request($Request, $Post = Null, $CustomRequest = Null)
108c108
< Func Request($Request, $Post = Null)
---
> Func Request($Request, $Post = Null, $CustomRequest = Null)
179a180,181
>     If Not IsKeyword($CustomRequest) Then Json_ObjPut($OptList, "customrequest", $CustomRequest)
> 
311a314
>     If Json_ObjExists($Obj, "customrequest") Then Curl_Easy_Setopt($Curl, $CURLOPT_CUSTOMREQUEST, Json_ObjGet($Obj, "customrequest"))
320a324
>

 PATCH 

Request.au3

Do you have request.au3 which include cUrl.au3 and json.au3 or where i can download it :(

thanks

Share this post


Link to post
Share on other sites
argumentum

Do you have request.au3 which include cUrl.au3 and json.au3 or where i can download it :(
thanks

ok, here they are, from my PC, in Curl there is a folder "changed by argumentum" and you may need the file from there if BinaryCall gives you trouble.
As soon as the files are back, I'll remove this post and attached files.

Request(2015.06.07).zip

Curl(2015.06.04).zip

Json(2015.01.08).zip

Share this post


Link to post
Share on other sites
fairylovehn127

 

What problem with my code, it return unsuccessfull request

Edited by fairylovehn127

Share this post


Link to post
Share on other sites
fairylovehn127

ok, here they are, from my PC, in Curl there is a folder "changed by argumentum" and you may need the file from there if BinaryCall gives you trouble.As soon as the files are back, I'll remove this post and attached files.

Request(2015.06.07).zip

Curl(2015.06.04).zip

Json(2015.01.08).zip

I want to implement curl like that. How can i put a header to curl :(. 

 

Edited by fairylovehn127

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • Surya
      By Surya
      Hi everyone its been loooong since I posted here 
      I have been trying to convert this curl executable parameters into autoit using the winhttp com object;
      curl -F data_file=@my_audio_file.mp3 -F model=en-US "https://api.speechmatics.com/v1.0/user/41049/jobs/?auth_token=MmQ5MTk4jdsgjhgghstOGU5YS00OWFhLWghdgjshgdhbshj017###" any ideas guys
       
      PS: I am excited to post here after a looong time
    • Seminko
      By Seminko
      I'm trying to get data from http://poe.trade/ - disclaimer, although this site is about a game, my script will not in any way interact directly with the game in any way. The script is just to get data from the site.
      To explain how it works - you submit a POST request and a custom URL is returned, then you do a GET request on that URL and you get the final URL you want.
       
      First issue:
      Now, I've tried doing so by using https://apitester.com/ and the first phase works. Here's how it looks like at APITester:
      Request Headers POST /search HTTP/1.1 Host: poe.trade Accept: */* User-Agent: Rigor API Tester Content-Length: 43 Content-Type: application/x-www-form-urlencoded Request Body online=x&name=kaom%27s%20heart&league=incursion When I submit this, the response I get is this:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>Redirecting...</title> <h1>Redirecting...</h1> <p>You should be redirected automatically to target URL: <a href="http://poe.trade/search/ioritewoteteme">http://poe.trade/search/ioritewoteteme</a>. If not click the link. So I then do a GET request for 'http://poe.trade/search/ioritewoteteme', which results in this response:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>Redirecting...</title> <h1>Redirecting...</h1> <p>You should be redirected automatically to target URL: <a href="http://poe.trade/search/inamotezuakito">http://poe.trade/search/inamotezuakito</a>. If not click the link. Great, this link (http://poe.trade/search/inamotezuakito) is exactly what we want.
      However, when I try to do the same in autoit, the result is quite different:
      Global Const $HTTP_STATUS_OK = 200 $test = HttpPost("http://poe.trade/search", "/online=x&name=kaom%27s%20heart&league=incursion") ClipPut($test) MsgBox(1, "", $test) Func HttpPost($sURL, $sData = "") Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1") $oHTTP.Open("POST", $sURL, False) If (@error) Then Return SetError(1, 0, 0) $oHTTP.SetRequestHeader("Host", "poe.trade") $oHTTP.SetRequestHeader("User-Agent", "Rigor API Tester") $oHTTP.SetRequestHeader("Accept", "*/*") $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded") $oHTTP.Send($sData) If (@error) Then Return SetError(2, 0, 0) If ($oHTTP.Status <> $HTTP_STATUS_OK) Then Return SetError(3, 0, 0) Return SetError(0, 0, $oHTTP.ResponseText) EndFunc The code above returns: ' 謟 '
      Any ideas as to what I am doing incorrectly?
       
      Second issue:
      Once I get the final link using APITester and do a GET on that i get a bunch of hieroglyphs. A friend of mine advised that the data is GZiped, which is a pain in the butt to be honest. However, apparently curl can uncompres that.
      How would I go about it?
       
      Thanks
    • Ascer
      By Ascer
      Hello
      Opertation Sys: Win7 x64
      Problem: Connecting to webs using TLS 1.1 +
      Description: WinHttp.WinHttpRequest.5.1 using TLS 1.0 by default, i need higher version to connect into some webs.
      Dim $oHttp = ObjCreate("WinHTTP.WinHTTPRequest.5.1") $oHttp.open ("GET", "https://howsmyssl.com/a/check", False) $oHttp.Option(9) = 128 ; 128 - TLS 1.0, 512 - TLS 1.1, 2048 - TLS 1.2, 2056 - TLS 1.1 & TLS 1.2 $oHttp.Send ConsoleWrite($oHttp.responseText & @CRLF) ; at end of the respond you can check your TLS version. Mine is: {"tls_version":"TLS 1.0","rating":"Bad"} Error: $oHttp.Option works only with parameter 128 (TLS 1.0) other values make error {Bad parameter}
      Additional: I've done this tutorial about enabling TLS in registry: <link>
      Thanks for support.
      Ascer
    • islandspapand
      By islandspapand
      Hi All
      i am currently trying to add a function to my project that can send SMS, i have gone with Twilio for the sms service that use a REST API.
      I have never worked with an API before, and could use some help.
      I can get my function working with using cURL.exe and copy past command from the website with the following code. And thats great unfortunately i am have issue with character like æøå when sending a SMS appears like a box or ?. this does not happen if i do it from the website so it looks like a Unicode issue in curl.exe.
      I have done some searching on the forum and understand that i should be able to implement this curl command with the WinHTTP UDF from @trancexx so i don't need a third part exe and it might fix my charater issue.
      Unfortunately i really don't understand how i am to change curl commands to the WinHTTP and i was hoping some good maybe give me an example i could learn from.
      Thanks in advanced
      i have removed the AuthToken number from the script.
      _SendSMS("00000000","SomeOne","SMS body info") Func _SendSMS($SendTo,$SendFrom,$Msgtxt) $AccountSID = "ACbb765b3180d5938229eff8b8f63ed1bc" $AuthToken = "Auth Token number" $Data = '"https://api.twilio.com/2010-04-01/Accounts/'&$AccountSID&'/Messages.json"'& _ '-X POST \ --data-urlencode "To=+45'&$SendTo&'" \ --data-urlencode "From='&$SendFrom&'" \ --data-urlencode "Body='&$Msgtxt&'" \ -u '&$AccountSID&':'&$AuthToken&'' ShellExecute(@ScriptDir&"\curl.exe","-k "&$Data) ;~ curl 'https://api.twilio.com/2010-04-01/Accounts/ACbb765b3180d5938229eff8b8f63ed1bc/Messages.json' -X POST \ ;~ --data-urlencode 'To=+4500000000' \ ;~ --data-urlencode 'From=Reception' \ ;~ --data-urlencode 'Body=Test Body' \ ;~ -u ACbb765b3180d5938229eff8b8f63ed1bc:[AuthToken] EndFunc  
       
    • jesus40
      By jesus40
      Hello friends, i have a working curl command that show informations about my account on binance.com, but_it dont work with autoit code without curl.exe.
      I want to do it without curl, because the whole process much Slower_ with StdoutRead (I want get the response in variable.)
      My Curl command in Autoit:
      This 2 are works, but_ i would like to do it without curl.exe
      $apikey="XYZ" sCommand = @ScriptDir & '\curl.exe -k -H "X-MBX-APIKEY: ' & $apikey & '" -X GET "https://api.binance.com/api/v3/account?' & $request the same in .bat  file
      curl.exe -k -H "X-MBX-APIKEY: XYZ" -X GET "https://api.binance.com/api/v3/account?timestamp=1514917812000&signature=85bdee77e53cd521e1d5229fbfb459d53799c42b3fa4596d73f1520fad5f965a" (I use curl with -k option which allows curl to make insecure connections, because there is problem with the sites certificate, (cURL error 60))
       
      I tried many variations, this is the latest... I cant get the same response.
      curl $error message (I changed ): {"code":-2015,"msg":"Invalid API-key, IP, or permissions for action."}
      autoit version $error message (Response code:400): Mandatory parameter 'timestamp' was not sent, was empty/null, or malformed.
       
      $request = $query & '&signature=' & $signature $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "https://api.binance.com/api/v3/account", False) $oHTTP.SetRequestHeader("X-MBX-APIKEY", $apikey) $oHTTP.Send($request) $oReceived = $oHTTP.ResponseText $oStatusCode = $oHTTP.Status If $oStatusCode <> 200 then MsgBox(4096, "Response code", $oStatusCode) EndIf  
      thanks
×