I'm trying to write a function to carry out a HTTP POST request to a https:// URL.
So far my code is:
GError *error = NULL;
GSocketClient *client = NULL;
GTlsConnection *tls_conn = NULL;
GSocketConnection *conn = NULL;
GIOStream *iostream = NULL;
GInputStream *input_stream = NULL;
GOutputStream *output_stream = NULL;
gsize bytes_read;
gsize response_length = 0;
// Create a GSocketClient for HTTPS
client = g_socket_client_new();
g_socket_client_set_tls(client, TRUE);
// Connect to the server
conn = g_socket_client_connect_to_uri(client, url, 443, NULL, &error);
if (!conn) {
g_error("Failed to connect: %s", error->message);
g_clear_error(&error);
goto cleanup;
}
// Get input and output streams
input_stream = g_io_stream_get_input_stream(G_IO_STREAM(conn));
output_stream = g_io_stream_get_output_stream(G_IO_STREAM(conn));
// Send the POST request
g_output_stream_write(output_stream, data, strlen(data), NULL, &error);
if (error) {
g_error("Failed to send POST data: %s", error->message);
g_clear_error(&error);
goto cleanup;
}
printf("Sent POST\n");
// Read the response
gchar response_data[4096];
while ((bytes_read = g_input_stream_read(input_stream, &response_data, 4096, NULL, &error)) > 0) {
response_length += bytes_read;
}
url and data are gchar*s passed to this function from the caller. I can copy/paste url and data and use them successfully with curl from the command line, so I know those are valid. I see the "Sent POST" printf output at stdout, but unfortunately the call to g_input_stream_read() never returns.
I haven't been able to find any good clear examples of how this should work, hopefully someone can point out my error. Thanks!