---
description: Simple HTTP/HTTPS requests, with some JSON as a bonus.
---

# HTTPLite

mkxp-z includes minimal support for communicating with HTTP servers. Support for unencrypted HTTP is always present; HTTPS requires building with the `use_https=true` option, and OpenSSL installed onto the system.

JSON and HTTP support are built on top of [json5pp](https://github.com/kimushu/json5pp) and [cpp-httplib](https://github.com/yhirose/cpp-httplib) respectively. All functions can raise `MKXPError`if something goes wrong.

#### JSON

* `HTTPLite::JSON.stringify(obj)` takes one object as an argument, transforming it into a JSON string. Accepts `Nil`, `Float`, `Integer`, `String`, `Array`, and `Hash`.
* `HTTPLite::JSON.parse(str)` takes one JSON string as an argument, transforming it back into its Ruby equivalent.

#### HTTP

The HTTP methods all return a hash as a response. This hash contains 3 members (at the moment):

* `:status (Int)` contains the response status (e.g. 200, 404)
* `:body (String)` contains the body portion of the server's response.
* `:headers (String => String)` contains a hash with all the headers contained in the server's response.

The HTTPLite module contains three functions. All return a hash formed in the way described above. They can all be optionally passed headers, as a hash with `String` values and `String` keys.

By default, these functions will attempt to follow redirect responses. This can be disabled by passing `false` to `follow_redirects`.

* `HTTPLite.get(url[, headers[, follow_redirects]])` takes a url as a string, performing a GET request.
* `HTTPLite.post(url, stringhash[, headers[, follow_redirects]])` takes a url as a string, and post data contained as a hash of string keys and string values, performing a POST request.
* `HTTPLite.post_body(url, bodycontent, contenttype[, headers[, follow_redirects]])` takes a url as a string, the request's body as a string, and the body's MIME-type as a string, performing a POST request.

```ruby
# Perform a GET request on any URL
response = HTTPLite.get("https://some.url")
if response[:status] == 200
    p response[:body]
end

# Perform a POST request on any URL
postdata = {
    "key1" => "value1",
    "key2" => "value2"
}
response = HTTPLite.post("https://some.url", postdata)

# Perform a POST request on any URL (using a body as data)
postdata = HTTPLite::JSON.stringify(postdata)
response = HTTPLite.post_body("https://some.url", postdata, "application/json")
```
