I am trying to write a REST Client in C. Currently it is standalone, but later on, I plan to port this to our codebase.
I am testing this on centos 6.5 with gcc.
I started of with code at https://gist.github.com/nolim1t/126991 as reference. This was suggested in quora by a couple of members.
I tweaked the above code to fit my needs. When I tried this, I am unable to get a valid response back from the rest server.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
int socket_connect(char *host, in_port_t port){
    struct hostent *hp;
    struct sockaddr_in addr;
    int on = 1, sock;
    if((hp = gethostbyname(host)) == NULL){
    herror("gethostbyname");
    exit(1);
}
    bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
    addr.sin_port = htons(port);
    addr.sin_family = AF_INET;
    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&on, sizeof(int));
    if(sock == -1){
        perror("setsockopt");
        exit(1);
    }
    if(connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1){
        perror("connect");
        exit(1);
    }
    return sock;
}
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]){
    int fd;
    int cx;
    char inputBuffer[BUFFER_SIZE];
    char buffer[BUFFER_SIZE];
    if(argc < 4){
        fprintf(stderr, "Usage: %s <hostname> <port> <other-args>\n", argv[0]);
        exit(1);
    }
    fd = socket_connect(argv[1], atoi(argv[2]));
    if(fd)
    {
        printf("socket success\n");
    }
    cx = snprintf(inputBuffer, BUFFER_SIZE, "GET %s\r\n", argv[3]);
//  snprintf(inputBuffer, argv[3], strlen(argv[3]));
    write(fd, inputBuffer, cx); // write(fd, char[]*, len);
    bzero(buffer, BUFFER_SIZE);
    printf("reading response\n");
    while(read(fd, buffer, BUFFER_SIZE - 1) != 0){
        fprintf(stderr, "%s", buffer);
        bzero(buffer, BUFFER_SIZE);
    }
    shutdown(fd, SHUT_RDWR);
    close(fd);
    return 0;
}
This is the rest like url that I am accessing: http://maps.googleapis.com/maps/api/geocode/json?address=chicago
When I go to it via the browser I am able to access the json data/response. However when I use the C program I get the following output.
#./rest maps.googleapis.com 80 /maps/api/geocode/json?address=chicago
socket success
reading response
And it stays there indefinitely.
I am attaching packet captures and the tcp stream when I access via the C program and also via the browser.
Edit:
Thanks for the responses. My original program worked. Debugged this and found that the corporate firewall was blocking the packets and hence I never got a response.
For the answer posted by @eyllanesc - This code works partially. For some unknown reason, the FIN packet from the server does not come immediately. It always takes 4 mins since the first data packet was sent. So the program keeps hanging for 4 minutes, before it terminates. Thanks for the effort though.



 
     
    