netPut()

The function netPut() allows you to upload data to a server.

Synopsis

int netPut(string url, mapping|string|blob data [, mapping &result]);

Parameter

Parameter Description
url

The URL (http, https, ftp and file - file only applies to local files).

Since the URL has to correspond to specific syntax rules, the QT implementation of the URL parser tries to correct errors in the syntax. See the section "QUrl::TolerantMode" in the description Qt - QURL Parsing Mode and Uniform Resource Identifier (URI)- Generic Syntax.

The function is terminated in case of an invalid URL and it returns -1.

data

The parameter "data" contains the data that is sent to server. The parameter can be a mapping, a string or a blob. Thus, 3 parameter types can be used. If a mapping is used as data, the keys described in the following can be used.

A netPut request receives an answer from the server (HTTP protocol). The content of the answer can be passed in memory to the result mapping (see the description on the next row of this table) or you can you specify a target file via the "target" key in the data mapping. See the keys for the data mapping below.

  • "target" (string) ... an absolute path to a directory or to a file the content of the response is saved in. If you specify a directory path, the file name from the URL is added to the directory path to create a final file name. This file name is also saved in the "result" variable under key word "target". For the variable result see the description on the next row of this table. The file must be opened for reading or the function returns -1 and is terminated. A file is created if it does not exist.

  • "content" ... Specifies the content that is transmitted. Permitted data types are string and blob. e.g..:

    data["content"] = "some data to transmit"; //
    string data["content"] =
    (blob)"736F6D65206461746120746F207472616E736D6974"; //
    blob ############# "result" Row:
    result["target"]:
  • "headers" (mapping) ... Additional header that is transferred to the server during the communication. In the key "Content-Type" in the mapping that can be passed in the "headers"key, you can specify the MIME type of the data transmitted. If you do no specify the data type, the content is automatically detected and a content type header is used. e.g.

    netPut("http://localhost:8080/data",
    makeMapping("content", "test text", "headers",
    makeMapping("Content-Type",
    "text/x-test")));
  • "ignoreSslErrors" ... If you pass a value unequal to dyn_string or an empty dyn_string, all SSL errors are ignored. All occurred SSL errors are, however, returned and you can find them in the result["sslErrors"] mapping. See the description of the result variable on the next row of this table and list of the SSL errors below the description. If you specify a dyn_string, you specify which SSL errors may be ignored. If there are other SSL errors, the function returns -1 and is terminated.

    data["ignoreSslErrors"] = ""; //All SSL errors are
    ignored data["ignoreSslErrors"] =
    makeDynString("CertificateUntrusted");//Ignore CertificateUntrusted errors
  • followRedirection" ...If you specify false for the bool parameter, the automatic "Redirection Handling" is deactivated. In the Redirection Handling, the HTTP protocol allows the server to send a "redirection" answer. Thereby, a client knows that the required resource is reachable under another URL. The function netGet follows these instructions by default. In order to avoid a redirection loop (e.g. A -> A oder A -> B -> C -> A, etc.), the number of answers is limited. If this number is exceeded, the function returns -1 ("RedirectionLoop" error) and is terminated. You can activate or deactivate the redirection handling by using the "followRedirection" key for the data mapping.

    e.g.:

    data["followRedirection"] = false; // automatic redirection deactivated
    data["followRedirection"] = true; // automatic redirection activated (default)
  • "sslConfig" ... the sslConfig is a mapping that contains the certificate information required for secure comminication and contains following keys:

    "localCertificate" (string) - PEM encoded certificate

    "privateKey" (mapping) - key and alogorithm information

    "key" (string) - private key in PEM encoded form.

    "alogorithm"(string) - The used alorithm, possible values: "RSA", "DSA", "EC" (Elliptic Curve)

    Example

    A PEM encoded certificate file can be loaded from the local disk like this:

    fileToString(getPath(CONFIG_REL_PATH,
                                                "certificate.pem"), cert);

    and then the returned string can be passed as value for the key "localCertificate".

  • clearAccessCache...Flushes the internal cache of authentication data and network connections. Note that this key must only be specified for the data mapping. You do not need to specify a value for it.

  • clearConnectionCache ... Flushes the internal cache of network connections. In contrast to clearAccessCache the authentication data is preserved. Note that this key must only be specified for the data mapping. You do not need to specify a value for it.

result

The function netPut receives an answer from the server and passes it the to result variable.

Thereby the following keys are optionally set. The keys with the text (always) are always set. See https://tools.ietf.org/html/rfc2616#section-6.1.1.

  • "headers" (always) ... Contains a mapping with key (header name) and value (header) pairs. It can also be empty.

    Example:.

    "Last-Modified" : "Mon, 09 Sep 2013 09:08:45 GMT"

    "Date" : "Wed, 16 Jul 2014 07:37:13 GMT"

    "Content-Length" : "1127"

    "Content-Type" : "image/png"

  • "content" (always) ... Contains the received data. The data type is string by default. You can specify the data type by using the variable data. If you specified a target file by using the "data" variable, the content here is always empty. See keyword "target" under data parameter above.

  • "url" (always) ... The URL that was used for the last query. It can differ from the original URL if a redirection answer is received from the server in the HTTP protocol (see Redirection Handling under data parameter on the row above).

  • "httpStatusCode", "httpStatusText" ... In an http/https communication the httpStatusCode and httpStatusText contain the last HTTP status code (int) and the respective "Reson Phrase" (string), e.g.

    "httpStatusCode" : 200

    "httpStatusText" : "OK"

    See https://tools.ietf.org/html/rfc2616#section-6.1.1

  • "target" ... If you specify a target file by using the data variable and the key word "target", the "target" key word here contains the absolute file path to the file/directory that contains the content of received answer. For data mapping see the row above in this table.

  • "errorString" ... The errorString contains a "readable" descriptive text if an error occurs in the communication. The parameters "error" and "errorString" are always set together. In this case, the function always returns -1.

  • "error" ... In case of errors, the function returns a string key word from the following list:

    - "RedirectionLoop" ... Too many redirects encountered

    - (see Redirection Handling)

    - All values from the enum are described on the following home page. The prefix string is not used, e.g. "ConnectionRefusedError"

    https://doc.qt.io/qt-5/qnetworkreply.html#NetworkError-enum

  • "sslErrors" ... If an SSL error occurs when establishing the communication, the list of SSL errors are passed in a mapping. The key in the mapping is a string key word from the following list. The value for the key is a descriptive text of the error. If the SSL errors are not ignored (see "ignoreSslErrors" under "data" mapping on the row above in this table), the function returns -1 and is terminated. Furthermore, the "result" variable does not contain any further keys.

    Possible SSL key words:

    "UnableToGetIssuerCertificate"

    "UnableToDecryptCertificateSignature"

    "UnableToDecodeIssuerPublicKey"

    "CertificateSignatureFailed"

    "CertificateNotYetValid"

    "CertificateExpired"

    "InvalidNotBeforeField"

    "InvalidNotAfterField"

    "SelfSignedCertificate"

    "SelfSignedCertificateInChain"

    "UnableToGetLocalIssuerCertificate"

    "UnableToVerifyFirstCertificate"

    "CertificateRevoked"

    "InvalidCaCertificate"

    "PathLengthExceeded"

    "InvalidPurpose"

    "CertificateUntrusted"

    "CertificateRejected"

    "SubjectIssuerMismatch"

    "AuthorityIssuerSerialNumberMismatch"

    "NoPeerCertificate"

    "HostNameMismatch"

    "NoSslSupport"

    "CertificateBlacklisted"

    "UnspecifiedError"

    The "result" mapping looks as follows:

    [mapping 1 items]

    "sslErrors" : mapping 4 items

    "CertificateUntrusted" : "The root CA certificate is not trusted for this purpose"

    "UnableToVerifyFirstCertificate" : "No certificates could be verified"

    "UnableToGetLocalIssuerCertificate" : "The issuer certificate of a locally looked up certificate could not be found"

    "HostNameMismatch" : "The host name did not match any of the valid hosts for this certificate"

  • "sslErrors2": Further information on the SSL errors such as certificate information.

    Possible key words:

    "errorString": Readable Errorstring "The certificate is self-signed, and untrusted"

  • "certificate": contains details about the certificate (if the error contains a certificate. Otherwise this "certificate" key does not exist)

    "digest": Unique identifier for each certificate e.g. "52bfdc363d0832f1a38cfcb612779bf69e4c8d8d"

    "text": certificate tex

Return value

The function returns 0 if it was successfully executed and in case of errors -1.

Description

The function netPut() allows uploading data to a server. The parameter "data" contains the data to be transmitted. The "data" parameter can be a mapping, a string or a blob. Thus, 3 parameter types can be used. If a mapping is used as data, keys described in the table above under "data" parameter are used.

/* The function netPut allows uploading data to a server.
IgnoreSslErros is used and since and empty string is passed, all SSL errors are ignored. */
main()
{
  mapping m;
  DebugN(netPut("http://localhost:80", makeMapping("content", "data to send", "followRedirection", false, "ignoreSslErrors", ""), m), m);
}

Assignment

HTTP functions

Availability

UI, CTRL