Network client (libcurl wrapper).
More...
#include <NetworkClient.h>
Inherits INetworkClient.
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
◆ 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
-
name | is the name of the request parameter |
fileName | is the physical path to the file |
displayName | is the display name (the name that is transferred to the server does not contain a path), |
contentType | is 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()
Example 1
nm.doGet("http://google.com/?q=" + nm.urlEncode("Smelly cat");
print(nc.responseBody());
Example 2
nm.setOutputFile("d:\\image.png");
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
nm.setUrl("https://www.googleapis.com/oauth2/v3/token");
nm.doPost("param1=value¶m=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";
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()
Returns the error text for the last request executed (for example, "HTTP 404 not found").
◆ getCurlInfoString()
string getCurlInfoString |
( |
int |
option | ) |
|
|
override |
◆ responseCode()
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 |
◆ 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 |
◆ urlEncode()
string urlEncode |
( |
string |
str | ) |
|
|
override |
Percent ecoding, it necessary when preparing a valid GET request.