Image Uploader Scripting API
Public Member Functions | List of all members
NetworkClient Class Reference

Network client (libcurl wrapper). More...

Inherits INetworkClient.

Public Member Functions

void addQueryHeader (string name, string value)
 
void addQueryParam (string name, string value)
 
void addQueryParamFile (string name, string fileName, string displayName, string contentType)
 
bool doGet (string url)
 
bool doPost (string data)
 
bool doUpload (string fileName, string data)
 
bool doUploadMultipartData ()
 
void enableResponseCodeChecking (bool enable)
 
string errorString ()
 
double getCurlInfoDouble (int option)
 
int getCurlInfoInt (int option)
 
string getCurlInfoString (int option)
 
int getCurlResult ()
 
string getCurlResultString ()
 
string responseBody ()
 
int responseCode ()
 
string responseHeaderByIndex (int index, string name)
 
string responseHeaderByName (string name)
 
int responseHeaderCount ()
 
string responseHeaderText ()
 
void setChunkOffset (double offset)
 
void setChunkSize (double size)
 
void setCurlOption (int option, string value)
 
void setCurlOptionInt (int option, long value)
 
void setMaxDownloadSpeed (uint speed)
 
void setMaxUploadSpeed (uint speed)
 
void setMethod (string str)
 
void setOutputFile (string str)
 
void setReferer (string str)
 
void setUrl (string url)
 
void setUserAgent (string userAgentStr)
 
string urlDecode (string str)
 
string urlEncode (string str)
 

Detailed Description

Network client (libcurl wrapper).

Note: After each completed request, most of the options are set to default values.

In .nut scripts: "nm" is a global instance of NetworkClient

Member Function Documentation

◆ addQueryHeader()

void addQueryHeader ( string   name,
string   value 
)
override

Sets the value of the HTTP request header. To delete a header, pass in an empty string. To set an empty value, pass "\n". Example:

nm.addQueryHeader("User-Agent", "Mozilla/5.0");

◆ addQueryParam()

void addQueryParam ( string   name,
string   value 
)
override

Adds a parameter to the POST request with the name and value

◆ addQueryParamFile()

void addQueryParamFile ( string   name,
string   fileName,
string   displayName,
string   contentType 
)
override

Adds a file parameter to the MULTIPART/DATA POST request.

Parameters
nameis the name of the request parameter
fileNameis the physical path to the file
displayNameis the display name (the name that is transferred to the server does not contain a path),
contentTypeis the mime file type, can be an empty string or obtained using the GetFileMimeType function). The method is similar to the HTML form element - <input type = "file">.

◆ doGet()

bool doGet ( string   url)
override

Example 1

nm.doGet("http://google.com/?q=" + nm.urlEncode("Smelly cat");
print(nc.responseBody());

Example 2

nm.setOutputFile("d:\\image.png"); //only UTF-8 file names are supported on Windows
nm.doGet("http://i.imgur.com/DDf2wbJ.png");

◆ doPost()

bool doPost ( string   data)
override

Performs a POST request.

Parameters
data- the request body (for example, "param1 = value param2 = value2"). If data is an empty string, the parameters previously set using the addQueryParam() function are used.

Example 1

nm.setUrl("https://www.googleapis.com/oauth2/v3/token");
nm.addQueryParam("refresh_token", refreshToken);
nm.addQueryParam("client_id", clientId);
nm.addQueryParam("client_secret", clientSecret);
nm.addQueryParam("grant_type", "refresh_token");
nm.doPost("");
print(nm.responseBody());

Example 2

//POST request from raw data
nm.setUrl("https://www.googleapis.com/oauth2/v3/token");
nm.doPost("param1=value&param=value");

◆ doUpload()

bool doUpload ( string   fileName,
string   data 
)
override

Sending a file or data directly in the body of a POST request

Example 1

nm.setMethod("PUT");
nm.setUrl("https://www.googleapis.com/drive/v2/files/" + id);
nm.addQueryHeader("Authorization", "Basic ");
nm.addQueryHeader("Content-Type", "application/json");
local postData = {title= "SmellyCat.jpg"};
nm.doUpload("", ToJSON(postData));

Example 2

local fileName = "c:\\test\\file.txt";
nc.setUrl("ftp://example.com");
nc.setMethod("PUT");
nc.doUpload(fileName, "");

◆ doUploadMultipartData()

bool doUploadMultipartData ( )
override

Sends a request to the address set by the function setUrl as parameters and files encoded in the MULTIPART/FORM-DATA format. Similar to sending a form with a file from a web page.

Example

local fileName = "c:\\test\\file.txt"; //only UTF-8 file names are supported on Windows
nm.setUrl("http://takebin.com/action");
nm.addQueryParamFile("file", fileName, ExtractFileName(FileName),"");
nm.addQueryParam("fileDesc", "cool file");
nm.doUploadMultipartData();
if ( nm.responseCode() == 200 ) {
print(nm.responseBody());
}

◆ enableResponseCodeChecking()

void enableResponseCodeChecking ( bool  enable)
override

Enables HTTP error logging.

◆ errorString()

string errorString ( )
override

Returns the error text for the last request executed (for example, "HTTP 404 not found").

◆ getCurlInfoString()

string getCurlInfoString ( int  option)
override

Get information from curl. curl_easy_getinfo. Numeric values of CURLINFO_* constants you can find here.

◆ responseCode()

int responseCode ( )
override

Returns the response code (for example, 200 means HTTP OK).

◆ responseHeaderCount()

int responseHeaderCount ( )
override

Returns number of headers in response.

◆ responseHeaderText()

string responseHeaderText ( )
override

Returns all response headers

◆ setChunkOffset()

void setChunkOffset ( double  offset)
override

Set the byte offset of current chunk, relative to the beginning of the full file.

Since
1.3.0

◆ setChunkSize()

void setChunkSize ( double  size)
override

Sets size of current chunk.

Since
1.3.0

◆ setCurlOption()

void setCurlOption ( int  option,
string   value 
)
override

Sets the string value for an option of the CURL object. Equivalent to calling the curl_easy_setopt function. Note that option is a number. A list of the numerical values of the option can be found here.

◆ setCurlOptionInt()

void setCurlOptionInt ( int  option,
long  value 
)
override

Sets the numeric value for an option of the CURL object

◆ setMaxUploadSpeed()

void setMaxUploadSpeed ( uint  speed)
override
Since
1.3.3

◆ setMethod()

void setMethod ( string   str)
override

Allows you to manually set the type of request - POST, GET, PUT ...

◆ setUrl()

void setUrl ( string   url)
override

Sets the URL for the next request.

◆ urlDecode()

string urlDecode ( string   str)
override
Since
1.3.2

◆ urlEncode()

string urlEncode ( string   str)
override

Percent ecoding, it necessary when preparing a valid GET request.