Coder Social home page Coder Social logo

sandia's Introduction

Overview

Sandia is a simple http only library made in C99, compiled with GCC 8.2 and tested under Antergos and Windows 10 x64.

Features

  • GET and POST requests
  • Add custom headers
  • Uses sockets, no third party libraries required

TODO

  • Better string handling
  • Response parsing (status codes, headers, content)

Notes

In Windows, link to ws2_32 library. If you are using Visual Studio this is not necessary because the pragma in sandia.h but in other compilers (GCC/MinGW) you must add the library in the linker options.

Examples

Simple GET request:

#include "sandia.h"
#include <stdio.h> // printf

sandia s = sandia_create("checkip.amazonaws.com" /* server host */, 80 /* server port */);
sandia_get_request(&s, "/" /* or "" */);
printf("%s\n", s.body);

Output:

HTTP/1.1 200 OK
Date: Wed, 31 Oct 2018 11:33:12 GMT
Server: lighttpd/1.4.41
Content-Length: 15
Connection: Close

XXX.XXX.XXX.XXX

GET request with headers:

#include "sandia.h"
#include <stdio.h> // printf

sandia s = sandia_create("httpbin.org", 80);

// add headers indivually
sandia_add_header(&s, "Referer", "https://github.com/blau72/sandia");

// or as an array
sandia_header headers[2] = {
  { "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" },
  { "Origin",     "https://github.com/blau72" }
};

sandia_add_headers(&s, headers, sizeof (headers) / sizeof (sandia_header));
        
sandia_get_request(&s, "/get?foo=bar");
printf("%s\n", s.body);

Output:

HTTP/1.1 200 OK
Connection: close
Server: gunicorn/19.9.0
Date: Wed, 31 Oct 2018 11:36:53 GMT
Content-Type: application/json
Content-Length: 383
Access-Control-Allow-Origin: https://github.com/blau72
Access-Control-Allow-Credentials: true
Via: 1.1 vegur

{
  "args": {
    "foo": "bar"
  }, 
  "headers": {
    "Connection": "close", 
    "Host": "httpbin.org", 
    "Origin": "https://github.com/blau72", 
    "Referer": "https://github.com/blau72/sandia", 
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
  }, 
  "origin": "XXX.XXX.XXX.XXX", 
  "url": "http://httpbin.org/get?foo=bar"
}

POST request:

#include "sandia.h"
#include <stdio.h> // printf

sandia s = sandia_create("httpbin.org", 80);

char content[] = "hello=world&happy=christmas";
sandia_post_request(&s, "/post?foo=bar", content, strlen(content));

printf("%s\n", s.body);

Output:

HTTP/1.1 200 OK
Connection: close
Server: gunicorn/19.9.0
Date: Wed, 31 Oct 2018 11:39:18 GMT
Content-Type: application/json
Content-Length: 307
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur

{
  "args": {
    "foo": "bar"
  }, 
  "data": "hello=world&happy=christmas", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Connection": "close", 
    "Content-Length": "27", 
    "Host": "httpbin.org"
  }, 
  "json": null, 
  "origin": "XXX.XXX.XXX.XXX", 
  "url": "http://httpbin.org/post?foo=bar"
}

sandia's People

Contributors

blau72 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

gh0st0ne

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.