http-web-request-get

Migrate from
https://cmakerhk.wordpress.com/2018/10/03/processing-web-request/


include the library by searching runemadsen

Sample code:

//Processing Code
import http.requests.*;

GetRequest get = new GetRequest("https://httpbin.org/ip");
get.send();
println("Reponse Content: " + get.getContent());
println("Reponse Content-Length Header: " + get.getHeader("Content-Length"));

result:

Reponse Content: {
  "origin": "203.85.99.9"
}

Reponse Content-Length Header: 30

Sample code2:

/**
 * HTTP Client. 
 * 
 * Starts a network client that connects to a server on port 80,
 * sends an HTTP 1.0 GET request, and prints the results. 
 *
 * Note that this code is not necessary for simple HTTP GET request:
 * Simply calling loadStrings("http://www.processing.org") would do
 * the same thing as (and more efficiently than) this example.
 * This example is for people who might want to do something more 
 * complicated later.
 */


import processing.net.*;

Client c;
String data;

void setup() {
  size(200, 200);
  background(50);
  fill(200);
  c = new Client(this, "httpbin.org", 80); // Connect to server on port 80
  c.write("GET /ip HTTP/1.0\r\n"); // Use the HTTP "GET" command to ask for a Web page
  c.write("Host: httpbin.org\r\n");
  c.write("\r\n");
}

void draw() {
  if (c.available() > 0) { // If there's incoming data from the client...
    data = c.readString(); // ...then grab it and print it
    println(data);
  }
}

result:

Client got end-of-stream.
HTTP/1.1 200 OK
Connection: close
Server: gunicorn/19.9.0
Date: Wed, 03 Oct 2018 07:01:44 GMT
Content-Type: application/json
Content-Length: 30
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur

{
  "origin": "203.85.99.9"
}


//linux code

curl -X GET "http://httpbin.org/get" -H "accept: application/json"

return:

{
  "origin": "203.85.99.9"
}

httpie:


WEB request full statement

GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
    1. A Request-line
    2. Zero or more header (General|Request|Entity) fields followed by CRLF
    3. An empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields
    4. Optionally a message-body
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
Request-URI = "*" | absoluteURI | abs_path | authority

ref:: https://www.tutorialspoint.com/http/http_requests.htm