Coder Social home page Coder Social logo

http-parser.mo's Introduction

HTTP Request Parser

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

Installation with mops

mops add https://github.com/NatLabs/http-parser.mo#v0.1.1

Usage

  • Import Module
    import HttpParser "mo:HttpParser";
  • Parse incoming http request
    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 {
        "<html><head><title> http_request </title></head><body><h1> Hello, " # name # "! </h1></body></html>"
    };

Check out the data types documentation for supported fields and methods

http-parser.mo's People

Contributors

skilesare avatar tomijaga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

http-parser.mo's Issues

Installing Via Mops.one

When trying to add http-parser to my project using mops add, it fails with the custom aviate-labs fields from package-set, for mo:/json

Arguably, you're doing a valid approach with the package-set for vessel, but mops doesn't parse the aviate labs ones successfully. Could we add a full mops.toml file with dependencies?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.