Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/NatLabs/http-parser.mo
- Owner: NatLabs
- License: mit
- Created: 2022-01-25T22:52:27.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-18T20:06:16.000Z (5 months ago)
- Last Synced: 2024-08-03T00:14:19.197Z (3 months ago)
- Topics: http, motoko
- Language: Motoko
- Homepage:
- Size: 96.7 KB
- Stars: 10
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-motoko - Http Request Parser - A http request parser for parsing url, search query, headers and form data. (Libraries / Web Programming)
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_requestHello, " # name # "!
"
};```
Check out the data types [documentation](./docs.md) for supported fields and methods