Skip to content

Commit

Permalink
Implement post() that takes post body
Browse files Browse the repository at this point in the history
  • Loading branch information
zoffixznet committed Jul 26, 2017
1 parent 25dfedb commit 1a6a4a0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion META6.json
Expand Up @@ -2,7 +2,7 @@
"perl" : "6.c",
"name" : "WWW",
"license" : "Artistic-2.0",
"version" : "1.002001",
"version" : "1.003001",
"description" : "No-nonsense, simple HTTPS client with JSON decoder",
"tags" : [ "HTTP" ],
"depends" : [
Expand Down
15 changes: 13 additions & 2 deletions README.md
Expand Up @@ -24,6 +24,13 @@ WWW - No-nonsense, simple HTTPS client with JSON decoder

# Same POST as above + decode response as JSON
say jpost('https://httpbin.org/post', :some<form-arg>)<form><some>;

# Also can post() or jpost() POST body directly as Str:D; headers passed as named args:
say jpost(
"http://httpbin.org/post",
to-json({:42a, :foo<meows>}),
:Authorization<Zofmeister>
).<json><foo>;
```

# DESCRIPTION
Expand Down Expand Up @@ -63,6 +70,7 @@ resultant data structure.
```perl6
multi post($url where URI:D|Str:D, *%form --> Str:D);
multi post($url where URI:D|Str:D, %headers, *%form --> Str:D);
multi post($url where URI:D|Str:D, Str:D $form-body, *%headers --> Str:D);

say post 'https://httpbin.org/post?meow=moo', :72foo, :bar<>;
say post 'https://httpbin.org/post?meow=moo',
Expand All @@ -75,12 +83,15 @@ included as named arguments. It's fine to also include query arguments in the
URL itself. Returns `Failure` if request fails or does not return a successful
HTTP code. Returns `Str` with the data on success.

To send POST body directly, pass it as Str:D positional arg. In this calling
form, the headers are sent as named args.

## `jpost`

```perl6
multi jpost($url where URI:D|Str:D, *%form --> Str:D);
multi jpost($url where URI:D|Str:D, %headers, *%form --> Str:D);
multi jpost($url where URI:D|Str:D, *%form);
multi jpost($url where URI:D|Str:D, %headers, *%form);
multi jpost($url where URI:D|Str:D, Str:D $form-body, *%headers);

say jpost 'https://httpbin.org/post?meow=moo', :72foo, :bar<>;
say jpost 'https://httpbin.org/post?meow=moo',
Expand Down
12 changes: 11 additions & 1 deletion lib/WWW.pm6
Expand Up @@ -13,7 +13,7 @@ sub get ($url) is export {
}
}

proto post ($, %?, *% ) is export {*}
proto post (|) is export {*}
multi post ($url, *%form) is export { post $url, %, |%form }
multi post ($url, %headers, *%form) is export {
CATCH { .fail }
Expand All @@ -22,5 +22,15 @@ multi post ($url, %headers, *%form) is export {
.decoded-content
}
}
multi post ($url, Str:D $json, *%headers) is export {
CATCH { .fail }
my $req = HTTP::Request.new: POST => $url, |%headers;
$req.add-content: $json;

with HTTP::UserAgent.new.request($req) {
.is-success or fail .&err;
.decoded-content;
}
}

sub err ($_) { "Error {.code}: {.status-line}" }
11 changes: 10 additions & 1 deletion t/01-operation.t
Expand Up @@ -5,7 +5,7 @@ use Test;
use Test::When <online>;
use WWW;

plan 8;
plan 10;

for <http https> -> $prot {
subtest 'get' => {
Expand Down Expand Up @@ -51,6 +51,15 @@ for <http https> -> $prot {
throws-like { post $prot ~ '://httpbin.org/status/404' }, Exception,
:message(/404/), "can detect a POST 404 over $prot.uc()";
}

subtest 'jpost body' => {
plan 2;
my $res = jpost "http://httpbin.org/post",
'{"a": 42, "foo": "meows"}',
:Authorization<Zofmeister>;
is-deeply $res.<json><foo>, 'meows', 'sent JSON in body matches';
is-deeply $res.<headers><Authorization>, 'Zofmeister', 'headers got sent';
}
}

for &get, &jget, &post, &jpost -> &s {
Expand Down

0 comments on commit 1a6a4a0

Please sign in to comment.