# 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")
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://roza-gb.gitbook.io/mkxp-z/extensions/httplite.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
