Vibe.d web interface tutorial

Daniel Kozak kozzi11 at gmail.com
Tue Mar 13 10:22:34 UTC 2018


On contrary I really hate that :D.
Because of this:

HTTP method Recognized prefixes
GET get, query
PUT set, put
POST add, create, post
DELETE remove, erase, delete
PATCH update, patch

I am calling vibed from PHP and I need to tweak my curl for every request
baceuse of this :D

 public function sendRequest($method, $params = []) {

        $method = strtolower(preg_replace('/([^A-Z])([A-Z])/', "$1_$2",
$method));
        $isGetRequestType = substr($method, 0, 3) == 'get';
        $isPutRequestType = substr($method, 0, 3) == 'set';
        $httpHeader = $isGetRequestType ? [] : ['Content-Type:
application/json'];
        if ($this->sessionId) {
            $httpHeader[] = "Cookie: vibe.session_id=" . $this->sessionId .
"; Path=/; HttpOnly";
        }

        if ($isGetRequestType) {
            $url =
UrlBuilder::fromUrl($this->url)->appendPath(substr($method, 4));
            $url->addParams($params);
            $curl = curl_init($url);
            curl_setopt_array($curl, [
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_HTTPHEADER => $httpHeader,
                CURLOPT_USERAGENT => EdiClient::USER_AGENT,
                CURLOPT_URL => $url->toString(),
                CURLOPT_RETURNTRANSFER => true
            ]);
        } elseif ($isPutRequestType) {
            $url =
UrlBuilder::fromUrl($this->url)->appendPath(substr($method, 4));
            $curl = curl_init($url);
            $json = json_encode($params);

            curl_setopt_array($curl, [
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_HTTPHEADER => $httpHeader,
                CURLOPT_USERAGENT => EdiClient::USER_AGENT,
                CURLOPT_URL => $url->toString(),
                CURLOPT_CUSTOMREQUEST => "PUT",
                CURLOPT_POSTFIELDS => $json,
                CURLOPT_RETURNTRANSFER => true
            ]);
        } else {
            $url = UrlBuilder::fromUrl($this->url)->appendPath($method);
            $curl = curl_init($url);
            $json = json_encode($params);

            curl_setopt_array($curl, [
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_HTTPHEADER => $httpHeader,
                CURLOPT_USERAGENT => EdiClient::USER_AGENT,
                CURLOPT_URL => $url->toString(),
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => $json,
                CURLOPT_RETURNTRANSFER => true
            ]);
        }

        $result = curl_exec($curl);
        curl_close($curl);
        $result = json_decode($result, true);

        return $result;
    }


On Tue, Mar 13, 2018 at 11:12 AM, Steven Schveighoffer via
Digitalmars-d-announce <digitalmars-d-announce at puremagic.com> wrote:

> On 3/9/18 11:34 AM, aberba wrote:
>
>> On Friday, 9 March 2018 at 16:32:28 UTC, aberba wrote:
>>
>>> New blog post for the learning audience
>>>
>>> aberba.com/2018/using-vibe-d-web-interface
>>>
>>
>> http://aberba.com/2018/using-vibe-d-web-interface
>>
>>
> Very nice! Although this is missing one of my favorite vibe.d web
> interface features -- automatic parsing of route parameters:
>
>
> class WebInterface
> {
>    void getRoute(HTTPServerResponse res, int foo, Nullable!int bar)
>    {
>        import std.format;
>        res.writeBody(format("foo = %s, bar = %s", foo, bar));
>    }
> }
>
> /route => Error, foo required
> /route?foo=1 => "foo = 1, bar = Nullable.null"
> /route?foo=2&bar=5 => "foo = 1, bar = 5"
>
> -Steve
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-announce/attachments/20180313/044d1247/attachment-0001.html>


More information about the Digitalmars-d-announce mailing list