httpSetMethodHandler()

The function can be used to redirect an HTTP method to a user-defined CTRL callback function.

Synopsis

int httpSetMethodHandler( string method, string callback);

Parameter

Parameter Description
method The HTTP method that should be redirected to a callback function: "CONNECT", "OPTIONS","TRACE","PUT" or "DELETE"
callback The callback function that is called.

Return value

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

Error

See above

Description

The function can be used to redirect an HTTP method ("CONNECT", "OPTIONS","TRACE","PUT","DELETE") to a user-defined CTRL callback function. In other words, when an HTTP server receives a message that contains one of these methods, the specified CTRL callback function is called. The function must contain the following arguments:

callback(string clientIP, string url, int idx);

Parameter Description
clientIP IP of the requesting client. See example below.
url The requested URL. See example below.
idx The client index, meaning an internal client number. See example below.

Example

The example redirects the HTTP method "DELETE" to the callback function "deleteCB".

#uses "CtrlHTTP"
main()
{
  dyn_string names, values;
  names = makeDynString("first", "second");
  values = makeDynString("first value", "second-value");
  DebugN(httpMakeParamString(names, values)); // =>
  "first=first%20value&second=second-value"
  //Example for httpSetMethodHandler
  httpServer(false, 8080, 0);
  httpSetMethodHandler("DELETE", "deleteCB");
  /* For this example, the client is the local computer and the
  client sends the HTTP DELETE message */
  string result;
  /* netDelete deletes the resource */
  netDelete("http://localhost:8080/path/to/document", result);
  if ( result == "done" )
  DebugN("resource was deleted");
}
string deleteCB(string clientIP, string url, int idx)
{
  DebugN("client " + clientIP + " has requested to delete " +
  url);
  return "done";
}

Assignment

CTRL PlugIn

Availability

CTRL