Uptooda Scripting API
Loading...
Searching...
No Matches
NetworkClient Class Reference

Network client (libcurl wrapper). More...

#include <NetworkClient.h>

Public Member Functions

void addPostField (string name, string value)
void addPostFieldFile (string name, string fileName, string displayName, string contentType)
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 (int offset)
void setChunkSize (int size)
void setCurlOption (int option, string value)
void setCurlOptionInt (int option, long value)
void setMethod (string str)
void setOutputFile (string str)
void setReferer (string str)
void setUploadAction ()
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

◆ addPostField()

void addPostField ( string name,
string value )
override

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

◆ addPostFieldFile()

void addPostFieldFile ( 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">.

◆ 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
Deprecated
Since 1.4.3. Misleading name (implies URL query params, but affects POST body). Use addPostField() instead.

◆ addQueryParamFile()

void addQueryParamFile ( string name,
string fileName,
string displayName,
string contentType )
override
Deprecated
Since 1.4.3. Misleading name (implies URL query params, but affects POST body). Use addPostFieldFile() instead.

◆ 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.addPostField("refresh_token", refreshToken);
nm.addPostField("client_id", clientId);
nm.addPostField("client_secret", clientSecret);
nm.addPostField("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 PUT 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.addPostFieldFile("file", fileName, ExtractFileName(FileName),GetFileMimeType(fileName));
nm.addPostField("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 ( int offset)
override

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

Since
1.3.0

◆ setChunkSize()

void setChunkSize ( int 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

◆ setMethod()

void setMethod ( string str)
override

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

◆ setUploadAction()

void setUploadAction ( )
override

Tells application that next request will perform file upload

◆ 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.