Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/NatLabs/http-parser.mo

HTTP Request Parser for Motoko
https://github.com/NatLabs/http-parser.mo

http motoko

Last synced: 1 day ago
JSON representation

HTTP Request Parser for Motoko

Awesome Lists containing this project

README

        

# HTTP Request Parser

A http request parser for parsing url, search query, headers and form data.

## Installation with [mops](https://mops.one/docs/install)

```
mops add http-parser
```

## Usage

- Import Module

```motoko
import HttpParser "mo:http-parser";
```

- Parse incoming http request

```motoko
public query func http_request(rawReq: HttpParser.HttpRequest) : async HttpParser.HttpResponse {

let req = HttpParser.parse(rawReq);
debugRequestParser(req);

let {url} = req;
let {path} = url;

switch (req.method, path.original){
case ("GET", "/"){
let optName = url.queryObj.get("name");
let name = Option.get(optName, "");
{
status_code = 200;
headers = [];
body = Text.encodeUtf8(htmlPage(name));
}
};
case (_){
{
status_code = 404;
headers = [];
body = Text.encodeUtf8("Page Not Found");
}
}
}
};

func htmlPage(name: Text): Text {
" http_request

Hello, " # name # "!

"
};

```

Check out the data types [documentation](./docs.md) for supported fields and methods