feat(http-post): adjust HTTP POST request

This commit is contained in:
dancingCycle 2023-03-24 14:50:27 +01:00
parent bcb06d4403
commit 4ce29da407
1 changed files with 32 additions and 26 deletions

View File

@ -415,14 +415,14 @@ int callback_update_entity (const struct _u_request * request, struct _u_respons
int callback_create_entity (const struct _u_request * request, struct _u_response * response, void * user_data) { int callback_create_entity (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/ /*declarations*/
int r; int r;
const char **keys; const char **keys=NULL;
const char *value; const char *value=NULL;
char *line; char *line=NULL;
int len; int len;
int i; int i;
PGconn *conn; PGconn *conn=NULL;
char *prepStmEntities; char *prepStmEntities=NULL;
PGresult *pqRes; PGresult *pqRes=NULL;
printf("callback_create_entity() Started...\n"); printf("callback_create_entity() Started...\n");
@ -437,22 +437,27 @@ int callback_create_entity (const struct _u_request * request, struct _u_respons
ulfius_set_string_body_response(response, 500, "This is the /entity/create route: HTTP POST data error!"); ulfius_set_string_body_response(response, 500, "This is the /entity/create route: HTTP POST data error!");
}else{ }else{
value = u_map_get(request->map_post_body, keys[0]); value = u_map_get(request->map_post_body, keys[0]);
printf("callback_create_entity() key is %s, value is %s\n", keys[0], value); if(value==NULL){
//TODO error
conn = (PGconn *)user_data; printf("callback_create_entity() key NOT available in request map\n");
//TODO Why this casting?
/*define prepared statement*/ (void)(request);
len=snprintf(NULL,0, (void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entity/create route: HTTP POST key error!");
}else{
printf("callback_create_entity() key is %s, value is %s\n", keys[0], value);
conn = (PGconn *)user_data;
/*define prepared statement*/
len=snprintf(NULL,0,
"INSERT INTO entities (name) VALUES ('%s') RETURNING *;" "INSERT INTO entities (name) VALUES ('%s') RETURNING *;"
,value); ,value);
prepStmEntities=malloc((size_t)(len+1)); prepStmEntities=malloc((size_t)(len+1));
//TODO Release prepStmEntities! //TODO Release prepStmEntities!
snprintf(prepStmEntities,(size_t)(len+1), snprintf(prepStmEntities,(size_t)(len+1),
"INSERT INTO entities (name) VALUES ('%s') RETURNING *;" "INSERT INTO entities (name) VALUES ('%s') RETURNING *;"
,value); ,value);
printf("callback_create_entity() prepStmEntities: %s\n",prepStmEntities); printf("callback_create_entity() prepStmEntities: %s\n",prepStmEntities);
/*create prepared statement*/
/*create prepared statement*/
/*conn:connection*/ /*conn:connection*/
/*stm:statement*/ /*stm:statement*/
/*0:number of passed parameters*/ /*0:number of passed parameters*/
@ -462,20 +467,21 @@ int callback_create_entity (const struct _u_request * request, struct _u_respons
/*NULL:relevant for binary parameters*/ /*NULL:relevant for binary parameters*/
/*0:obtain result in text format*/ /*0:obtain result in text format*/
//TODO How to pass params? //TODO How to pass params?
pqRes = PQexecParams(conn, prepStmEntities, 0, NULL, NULL, NULL, NULL, 0); pqRes = PQexecParams(conn, prepStmEntities, 0, NULL, NULL, NULL, NULL, 0);
if(PQresultStatus(pqRes) != PGRES_TUPLES_OK) { if(PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
/*handle error*/ /*handle error*/
printf("callback_create_entity() No data retrieved\n"); printf("callback_create_entity() No data retrieved\n");
ulfius_set_string_body_response(response, 500, "This is the /entity/create route: Dba error!"); ulfius_set_string_body_response(response, 500, "This is the /entity/create route: Dba error!");
/*clean up result*/ /*clean up result*/
PQclear(pqRes); PQclear(pqRes);
//TODO Why this casting? //TODO Why this casting?
}else{ }else{
/*evaluate result*/ /*evaluate result*/
int nbRecords=PQntuples(pqRes); int nbRecords=PQntuples(pqRes);
printf("callback_get_entity() nbRecords: %d\n",nbRecords); printf("callback_get_entity() nbRecords: %d\n",nbRecords);
/*clean up result*/ /*clean up result*/
PQclear(pqRes); PQclear(pqRes);
}
} }
} }
}else{ }else{