Monkey Documentation

Class HttpRequest

The httprequest module allows you to perform http requests such as GET and POST. More...


Constructors:
  • New ()
  • New ( requestMethod:String, url:String, onComplete:IOnHttpRequestComplete )
Methods:
  • BytesReceived : Int ()
  • Open : Void ( requestMethod:String, url:String, onComplete:IOnHttpRequestComplete )
  • ResponseText : String ()
  • Send : Void ()
  • Send : Void ( data:String, mimeType:String="text/plain;charset=utf-8", encoding:String="utf8" )
  • SetHeader : Void ( name:String, value:String )
  • Status : Int ()

Detailed Discussion

The httprequest module allows you to perform http requests such as GET and POST.

Example

Import mojo

Import brl.httprequest

Class MyApp Extends App Implements IOnHttpRequestComplete

    Field get_req:HttpRequest,post_req:HttpRequest
    
    Method OnHttpRequestComplete:Voidreq:HttpRequest )
    
        If req=get_req
            Print "Http GET complete!"
        Else
            Print "Http POST complete!"
        Endif

        Print "Status="+req.Status()
        Print "ResponseText="+req.ResponseText()
        
    End
    
    Method OnCreate()
    
        get_req=New HttpRequest"GET","http://posttestserver.com",Self )
        get_req.Send
        
        post_req=New HttpRequest"POST","http://posttestserver.com/post.php",Self )
        post_req.Send "Hello World!"
            
        SetUpdateRate 60
    End
    
    Method OnUpdate()

        If KeyHitKEY_CLOSE ) Error ""

        UpdateAsyncEvents
    End

    Method OnRender()
    
        Cls
        
        DrawText "Http GET bytes received="+get_req.BytesReceived(),0,0
        DrawText "Http POST bytes received="+post_req.BytesReceived(),0,12

    End 
End

Function Main()

    New MyApp

End


Constructor Documentation

Method New ()

Creates a new HttpRequest object.

Method New ( requestMethod:String, url:String, onComplete:IOnHttpRequestComplete )

Creates a new http request object and opens it.

The requestMethod parameter should be GET, HEAD, POST, PUT, DELETE or any http method recognized by the server.

See also Open


Method Documentation

Method BytesReceived : Int ()

Returns the total number of bytes received.

Method Open : Void ( requestMethod:String, url:String, onComplete:IOnHttpRequestComplete )

Opens an http request.

This effectively 'resets' the internal state of the http request.

The requestMethod parameter should be GET, HEAD, POST, PUT, DELETE or any http method recognized by the server.

Method ResponseText : String ()

The response text returned by the server once the request has completed.

Method Send : Void ()

Sends an http request.

Once complete, the OnHttpRequestComplete method of the IOnHttpRequestComplete object passed to the construtor will be called.

Your application must continously call UpdateAsyncEvents at regular intervals (for example, once per OnUpdate) while an http request operation is in progress.

Method Send : Void ( data:String, mimeType:String="text/plain;charset=utf-8", encoding:String="utf8" )

Sends an http request with data.

Send sets the request's Content-Length header to the encoded length of data.

If mimeType is not an empty string, Send also sets the request Content-Type header.

Once complete, the OnHttpRequestComplete method of the IOnHttpRequestComplete object passed to the construtor will be called.

Your application must continously call UpdateAsyncEvents at regular intervals (for example, once per OnUpdate) while an http request operation is in progress for it to complete.

Method SetHeader : Void ( name:String, value:String )

Sets an http request header option.

Method Status : Int ()

The status returned by the server once the request has completed.