Skip to content

Instantly share code, notes, and snippets.

@robinvanemden
Forked from leiless/postman-echo.c
Created April 28, 2021 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinvanemden/f0dfae32e75b05ebc10f767959e9b247 to your computer and use it in GitHub Desktop.
Save robinvanemden/f0dfae32e75b05ebc10f767959e9b247 to your computer and use it in GitHub Desktop.
[Test] Simple program to POST a HTTP request to postman-echo.com
/**
* Simple program to POST a HTTP request
*
* see:
* https://docs.postman-echo.com/?version=latest
* https://aticleworld.com/http-get-and-post-methods-example-in-c/
* https://stackoverflow.com/questions/11208299/how-to-make-an-http-get-request-in-c-without-libcurl
* https://stackoverflow.com/questions/22077802/simple-c-example-of-doing-an-http-post-and-consuming-the-response
*/
#include <stdio.h> /* printf, sprintf */
#include <stdlib.h> /* exit */
#include <unistd.h> /* read, write, close */
#include <string.h> /* memcpy, memset */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */
static void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
static int port = 80;
static const char *host = "postman-echo.com";
static const char *message_fmt = "POST /post?data=%s HTTP/1.0\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %zu\r\n"
"\r\n%s";
struct hostent *server;
struct sockaddr_in serv_addr;
int fd, bytes, sent, received, total;
char message[1024], response[4096];
if (argc < 3) {
puts("Usage:\n\t<data> <body>\n");
exit(0);
}
sprintf(message, message_fmt, argv[1], strlen(argv[2]), argv[2]);
printf("Request:\n%s\n", message);
/* Create the socket */
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) error("ERROR opening socket");
/* Lookup the ip address */
server = gethostbyname(host);
if (server == NULL) error("ERROR, no such host");
/* Fill in the structure */
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
/* Connect the socket */
if (connect(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
/* Send the request */
total = strlen(message);
sent = 0;
do {
bytes = write(fd, message + sent, total - sent);
if (bytes < 0)
error("ERROR writing message to socket");
if (bytes == 0) break;
sent += bytes;
} while (sent < total);
/* Receive the response */
memset(response, 0, sizeof(response));
total = sizeof(response) - 1;
received = 0;
do {
bytes = read(fd, response + received, total - received);
if (bytes < 0)
error("ERROR reading response from socket");
if (bytes == 0) break;
received += bytes;
} while (received < total);
if (received == total)
error("ERROR storing complete response from socket");
(void) close(fd);
/* process response */
printf("Response:\n%s\n", response);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment