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 (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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 (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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

    • 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
    • wakillon
      By wakillon
      HttpHeaderWatcher v1.0.1.3
       

      Some time ago, some members asked how to see the Http Requests.
      There are quite a few external applications but not in AutoIt!
      HttpHeaderWatcher in association with WinPcap, very modestly solves this lack.
       
      Once done this http watcher, i asked me : why not Re-build a WinHttp Request in AutoIt from a selected Request in one Click ?
      So i have added a "Create au3" button who open the WinHttp Request of your choice in AutoIt format in SciTE Editor.
      Voila voila, hope it can help ! 
      Buttons were made online with chimply.com the easy and free buttons generator !
      See Help for more infos.
      previous downloads : 253
      source and executable are available in the Download Section
      Hope you like it !
×