I am currently trying to send a full POST request with C to my local server where my PHP page will consume the request and INSERT the request into the database.
If I use the form I created to insert into the database, it works perfect.
But I'm trying to do the same POST request but in C to INSERT INTO the database. I'm not sure how to make the POST request. I intercepted the request with Burp so I know the request will be perfect and work. I'm just not sure how to implement it in my C program.
I have included my current C code to send the HTTP POST request.
I get a good reply, but nothing inserts into the database
/*
Create a TCP socket
*/
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET s;
    struct sockaddr_in server;
    char *message , server_reply[2000];
    int recv_size;
    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }
    printf("Initialised.\n");
    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }
    printf("Socket created.\n");
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons(80);
    //Connect to remote server
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }
    puts("Connected");
    char* str2;
    char* str3;
    char* str5;
    char* str20;
    str2 = "POST /project2/public/addJoke.php HTTP/1.0\r\n";
    str3 = "Content-Type: text/plain\r\n";
    str20= "Content-Length: 12\r\n\r\n";
    str5 = "joketext=ctest&jokedate=2018-01-01";
    char * message2 = (char *) malloc(1 + strlen(str2)+ strlen(str3)+ strlen(str5) + strlen(str20));
    strcpy(message2,str2);
    strcat(message2,str3);
    strcat(message2,str20);
    strcat(message2,str5);
    if( send(s , message2 , strlen(message2) , 0) < 0)
    {
        puts("Send failed");
        return 1;
    }
    puts("Data Send\n");
    //Receive a reply from the server
    if((recv_size = recv(s , server_reply , 2000 , 0)) == SOCKET_ERROR)
    {
        puts("recv failed");
    }
    puts("Reply received\n");
    //Add a NULL terminating character to make it a proper string before printing
    server_reply[recv_size] = '\0';
    puts(server_reply);
    return 0;
}
And here is my PHP file that receives the request.
if(isset($_POST['joketext'])){
try{
include __DIR__ . '/../includes/databaseconnection.php';
$title = 'Good Connection';
$sql = 'INSERT INTO jokes.jokes_tbl SET
joketext = :joketext,
jokedate = CURDATE(),
authorid = :authorid';
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':joketext', $_POST['joketext']);
$stmt->bindValue(':authorid', $_POST['authorid']);
$stmt->execute();
header('location: jokeslist.php');
}catch(PDOException $err){
$title = 'Bad Connection';
$output = 'Bad Connection' . 
$err->getMessage() . ' in ' . 
$err->getFile() . ':' . $err->getLine(); 
}
}else{
 ob_start();
include __DIR__ . '/../templates/addJoke.html.php';
$output = ob_get_clean();
}
include __DIR__ . '/../templates/layout.html.php';
 
    