Hello,
The CTRL function example() is called if a client requests the resource "/endpoint_example".
httpConnect("example", "/endpoint_example");
How can I detect the type of callback function I need to use?
Whether the 7-parameter version or the 6-parameter version.
GET (7 parameter)
string|dyn_string workCB([dyn_string names [, dyn_string values [, string user [, string ip [, dyn_string headerNames [, dyn_string headerValues [, int connectionIndex]]]]]]])
POST (6 parameter)
string workCB([string content [, string user [, string ip [, dyn_string headerNames [, dyn_string headerValues [, int connectionIndex]]]]]]])
string workCB([blob content [, string user [, string ip [, dyn_string headerNames [, dyn_string headerValues [, int connectionIndex]]]]]])
Best regards,
Luis Miguel BP.
HTTP Server CTRL functions - httpConnect
- luis.bustamante
- Posts:30
- Joined: Thu Jan 24, 2019 8:11 am
HTTP Server CTRL functions - httpConnect
- kilianvp
- Posts:443
- Joined: Fri Jan 16, 2015 10:29 am
Re: HTTP Server CTRL functions - httpConnect
It depends if you do a HTTP POST or HTTP GET.
- luis.bustamante
- Posts:30
- Joined: Thu Jan 24, 2019 8:11 am
Re: HTTP Server CTRL functions - httpConnect
A given Web-Resource can only call one CTRL-function, that means a second call to httpConnect() with the same Web-Resource name with lead to an error, and -1 is returned.
f I have an endpoint/resource that wants to receive GET and POST requests, the type of callback functions that I can associate to them are different.
I want the api rest way:
httpConnect("callbackArticle", "/api/v1/article"); // Receive GET and POST requests.
I think that the OA actual way is:
httpConnect("callbackArticleGet", "/api/v1/article_get"); // Receive GET requests, callback with 7 parameter.
httpConnect("callbackArticlePost", "/api/v1/article_post"); // Receive POST requests, callback with 6 parameter.
f I have an endpoint/resource that wants to receive GET and POST requests, the type of callback functions that I can associate to them are different.
I want the api rest way:
httpConnect("callbackArticle", "/api/v1/article"); // Receive GET and POST requests.
I think that the OA actual way is:
httpConnect("callbackArticleGet", "/api/v1/article_get"); // Receive GET requests, callback with 7 parameter.
httpConnect("callbackArticlePost", "/api/v1/article_post"); // Receive POST requests, callback with 6 parameter.
- luis.bustamante
- Posts:30
- Joined: Thu Jan 24, 2019 8:11 am
Re: HTTP Server CTRL functions - httpConnect
Ok, here is a possible solution.
httpConnect("cbDeviceSet", basePath + "/device/create", contentType_json); // POST
httpConnect("cbDeviceGet", basePath + "/device/read", contentType_json); // GET
httpConnect("cbDeviceSet", basePath + "/device/update", contentType_json); // POST
httpConnect("cbDeviceSet", basePath + "/device/delete", contentType_json); // POST
private static dyn_string cbDeviceGet(dyn_string requestNames, dyn_string requestValues, string requestUser, string requestIP, dyn_string headerNames, dyn_string headerValues, int idx)
{
// Validate request method.
if (getType(idx) != INT_VAR || idx == "" || (httpGetMethod(idx) != "GET" && httpGetMethod(idx) != "HEAD")) { return getResponseInvalidRequestMethod(); }
// HTTP Request parameters.
const mapping parameters = getRequestParameters(requestUser, requestIP, idx);
// TO-DO.
// HTTP Response.
const mapping response = makeMapping("status", 200, "success", true, "code", 0, "message", "API REST Demo", payload", "{}");
return makeDynString(jsonEncode(response), getHttpStatusMessage(200));
}
private static dyn_string cbDeviceSet(mixed content, const string requestUser, const string requestIP, const dyn_string headerNames, const dyn_string headerValues, const int idx)
{
// Validate request method.
if (getType(idx) != INT_VAR || idx == "" || httpGetMethod(idx) != "POST") { return getResponseInvalidRequestMethod(); }
// HTTP Request parameters.
const mapping parameters = getRequestParameters(requestUser, requestIP, idx);
// Parse request JSON content.
content = getRequestPostContent(content);
// TO-DO.
const string requestedURI = parameters["requestedURI"];
if (requestedURI.endsWith("/create"))
{
DebugTN(baseName(__FILE__), __FUNCTION__, __LINE__, "requestedURI", requestedURI, "content", content);
}
else if (requestedURI.endsWith("/update"))
{
DebugTN(baseName(__FILE__), __FUNCTION__, __LINE__, "requestedURI", requestedURI, "content", content);
}
else if (requestedURI.endsWith("/delete"))
{
DebugTN(baseName(__FILE__), __FUNCTION__, __LINE__, "requestedURI", requestedURI, "content", content);
}
// HTTP Response.
const mapping response = makeMapping("status", 200, "success", true, "code", 0, "message", "API REST Demo", "payload", "{}");
return makeDynString(jsonEncode(response), getHttpStatusMessage(200));
}
httpConnect("cbDeviceSet", basePath + "/device/create", contentType_json); // POST
httpConnect("cbDeviceGet", basePath + "/device/read", contentType_json); // GET
httpConnect("cbDeviceSet", basePath + "/device/update", contentType_json); // POST
httpConnect("cbDeviceSet", basePath + "/device/delete", contentType_json); // POST
private static dyn_string cbDeviceGet(dyn_string requestNames, dyn_string requestValues, string requestUser, string requestIP, dyn_string headerNames, dyn_string headerValues, int idx)
{
// Validate request method.
if (getType(idx) != INT_VAR || idx == "" || (httpGetMethod(idx) != "GET" && httpGetMethod(idx) != "HEAD")) { return getResponseInvalidRequestMethod(); }
// HTTP Request parameters.
const mapping parameters = getRequestParameters(requestUser, requestIP, idx);
// TO-DO.
// HTTP Response.
const mapping response = makeMapping("status", 200, "success", true, "code", 0, "message", "API REST Demo", payload", "{}");
return makeDynString(jsonEncode(response), getHttpStatusMessage(200));
}
private static dyn_string cbDeviceSet(mixed content, const string requestUser, const string requestIP, const dyn_string headerNames, const dyn_string headerValues, const int idx)
{
// Validate request method.
if (getType(idx) != INT_VAR || idx == "" || httpGetMethod(idx) != "POST") { return getResponseInvalidRequestMethod(); }
// HTTP Request parameters.
const mapping parameters = getRequestParameters(requestUser, requestIP, idx);
// Parse request JSON content.
content = getRequestPostContent(content);
// TO-DO.
const string requestedURI = parameters["requestedURI"];
if (requestedURI.endsWith("/create"))
{
DebugTN(baseName(__FILE__), __FUNCTION__, __LINE__, "requestedURI", requestedURI, "content", content);
}
else if (requestedURI.endsWith("/update"))
{
DebugTN(baseName(__FILE__), __FUNCTION__, __LINE__, "requestedURI", requestedURI, "content", content);
}
else if (requestedURI.endsWith("/delete"))
{
DebugTN(baseName(__FILE__), __FUNCTION__, __LINE__, "requestedURI", requestedURI, "content", content);
}
// HTTP Response.
const mapping response = makeMapping("status", 200, "success", true, "code", 0, "message", "API REST Demo", "payload", "{}");
return makeDynString(jsonEncode(response), getHttpStatusMessage(200));
}