title: REST Client in C
body: <p>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.</p>
  
  <p>I am testing this on centos 6.5 with gcc.</p>
  
  <p>I started of with code at <a href="https://gist.github.com/nolim1t/126991" rel="nofollow noreferrer">https://gist.github.com/nolim1t/126991</a> as reference. This was suggested in quora by a couple of members.</p>
  
  <p>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.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;netinet/tcp.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  
  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-&gt;h_addr, &amp;addr.sin_addr, hp-&gt;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 *)&amp;on, sizeof(int));
  
      if(sock == -1){
          perror("setsockopt");
          exit(1);
      }
  
      if(connect(sock, (struct sockaddr *)&amp;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 &lt; 4){
          fprintf(stderr, "Usage: %s &lt;hostname&gt; &lt;port&gt; &lt;other-args&gt;\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;
  }
  </code></pre>
  
  <p>This is the rest like url that I am accessing:
  <a href="http://maps.googleapis.com/maps/api/geocode/json?address=chicago" rel="nofollow noreferrer">http://maps.googleapis.com/maps/api/geocode/json?address=chicago</a></p>
  
  <p>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.</p>
  
  <pre><code>#./rest maps.googleapis.com 80 /maps/api/geocode/json?address=chicago
  socket success
  reading response
  </code></pre>
  
  <p>And it stays there indefinitely.</p>
  
  <p>I am attaching packet captures and the tcp stream when I access via the C program and also via the browser.</p>
  
  <p><a href="https://i.stack.imgur.com/F5DgM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F5DgM.png" alt="Packet Capture when I use the C program"></a></p>
  
  <p><a href="https://i.stack.imgur.com/b9sAa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b9sAa.png" alt="TCP stream for the C program request"></a></p>
  
  <p><a href="https://i.stack.imgur.com/S33wp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S33wp.png" alt="packet capture for browser request"></a></p>
  
  <p>Edit:</p>
  
  <p>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.</p>
  
  <p>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.</p>