This object can be used to handle Http requests consistently across platforms. There are two intended usages:


Lib.Sys.Net.Http.new(url)

Creates a new Http instance with url as parameter.

This does not do a request until request() is called.

If url is nil, the field url must be set to a value before making the call to request(), or the result is unspecified.

url - url for http request

Lib.Sys.Net.Http.requestUrl(url)

make http request with url and return result as string

url - url for http request


Settings properties:

PROXY

if request require proxy settings. Can be set once:

Lib.Sys.Net.Http.PROXY = { host = 'String', port = 8080, auth = { user = 'String', pass = 'String' } }

table with fields:

{ host = String, port = Int, auth = { user = String, pass = String } } 


Created object has properties:

cnxTimeout

socket timeout

float

noShutdown

if true do not shutdown socket

true/false

responseData

response result in string

responseData, string

url

The url of this request. It is used only by the request() method and can be changed in order to send the same request to different target Urls.

string

responseHeaders

return http response headers map

library map object (check Examples how to get response header values)


Created object methods:

addHeader(headervalue)

Add the header identified as header to value value.

If header or value are nil, the result is unspecified.
This method provides a fluent interface.

header - header key

value - header key value

addParameter(param, value)

Add the parameter identified as param to value value.

If header or value are nil, the result is unspecified.
This method provides a fluent interface.

param - parameter key

value - parameter key value

customRequest(postapisockmethod)

Sends this Http custom request to the Url specified by .url using sock Socket object.

If post is true, the request is sent as POST request, otherwise it is sent as method request. If method is nil then post will identify request method.

Response will stream to api Output object.

post - true/false method POST

api - Output object, can be FileOutput, BytesOutput

sock - Socket object

method - string GET, POST, PUT, etc.

fileTransfer(argnamefilenamefilesize, mimeType)

Prepare file info for http transfer using file Input stream including filename, size and mimeType information. Additional argname will be added as multipart form-data name.

argname -  multipart form-data name (Content-Disposition: form-data; name="argname")

filename - file name

file - Input Object, can be FileInput, BytesInput

mimeType - sting mime type, usually application/octet-stream 

onData(data)

This method is called upon a successful request, with data containing the result string.

The intended usage is to bind it to a custom function:

httpInstance.onData = function(data)

  print(data)

end

data - request result text

onBytesData(bytes)

This method is called upon a successful request, with data containing the result Bytes.

The intended usage is to bind it to a custom function:

httpInstance.onBytesData = function(bytes)

  print(bytes.length)

end

bytes - request result bytes

onError(msg)

This method is called upon a request error, with msg containing the error description.

The intended usage is to bind it to a custom function:

httpInstance.onError = function(msg)

    print(msg)

end

msg - error text

onStatus(status)

This method is called upon a Http status change, with status being the new status.

The intended usage is to bind it to a custom function:

httpInstance.onStatus = function(status)

   print(status)

end

status - status code

request(post)

Sends this Http request to the Url specified by .url.

If post is true, the request is sent as POST request, otherwise it is sent as GET request.

Depending on the outcome of the request, this method calls the onStatus(), onError() or onData() callback functions.

If .url is nil, the result is unspecified.

If .url is an invalid or inaccessible Url, the onError() callback function is called.

post - true/false method POST

setHeader(headervalue)

Sets the header identified as header to value value.
If header or value are nil, the result is unspecified.
This method provides a fluent interface.

header - header key

value - header key value

setParameter(paramvalue)

Sets the parameter identified as param to value value.
If header or value are nil, the result is unspecified.
This method provides a fluent interface.

param - parameter key

value - parameter key value

setPostBytes(data)

Sets the post data of this Http request to data bytes. 

There can only be one post data per request. Subsequent calls to this method or to setPostData() overwrite the previously set value.

If data is nil, the post data is considered to be absent.

This method provides a fluent interface.

data - Bytes, data to be posted.

setPostData(data)

Sets the post data of this Http request to data.

There can only be one post data per request. Subsequent calls overwrite the previously set value.

If data is nil, the post data is considered to be absent.

This method provides a fluent interface.

data - string, data to be posted


NOTE: Http object doesn't support automatic redirects. Use URLLoader for this.


Examples:


url = 'https://www.w3.org/'

print( 'Sending http request to '..url )


r = Lib.Sys.Net.Http.new( url )

r.onData = function(data)

 local respHeaders = r.responseHeaders

 local keys = respHeaders.keys() 

 while keys.hasNext() do

   local key = keys.next()

   print(key.."="..respHeaders.get(key))

 end 

end

r.onBytesData = function(bytes)

  print("bytes length: "..bytes.length)

  print("first byte: "..bytes.get(0))

end

r.onError = print

r.request(false)

Created with the Personal Edition of HelpNDoc: Produce electronic books easily