Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make HTTP requests with Raku?

Tags:

raku

How do you make HTTP requests with Raku? I'm looking for the equivalent of this Python code:

import requests

headers = {"User-Agent": "python"}
url = "http://example.com/"
payload = {"hello": "world"}

res = requests.get(url, headers=headers)
res = requests.post(url, headers=headers, json=payload)
like image 483
R891 Avatar asked Nov 25 '20 07:11

R891


People also ask

How do you make HTTP requests?

The most common HTTP request methods have a call shortcut (such as http. get and http. post), but you can make any type of HTTP request by setting the call field to http. request and specifying the type of request using the method field.

What is HTTP request library?

Introduction The HTTP Requests library provides a clean and simplified interface to make HTTP requests. The spirit of this library is to make it trivial to do easy things with HTTP requests. It can accomplish the most common use cases for an HTTP client, but does not aim to be a complete HTTP client implementation.


Video Answer


3 Answers

You may want to try out the recent HTTP::Tiny module.

use HTTP::Tiny;
my $response = HTTP::Tiny.new.get( 'https://example.com/' );
say $response<content>.decode
like image 77
Scimon Proctor Avatar answered Oct 17 '22 21:10

Scimon Proctor


After searching around a bit, I found an answer in the Cro docs.

use Cro::HTTP::Client;

my $resp = await Cro::HTTP::Client.get('https://api.github.com/');
my $body = await $resp.body;

# `$body` is a hash
say $body;

There's more information on headers and POST requests in the link.

like image 30
R891 Avatar answered Oct 17 '22 20:10

R891


I want to contribute a little more. There is a fantastic module named WWW. It's very convenient to make 'gets' that receive json because it can be parsed automagically.

In their example:

use WWW;
my $response = jget('https://httpbin.org/get?foo=42&bar=x');

You can examine the objects using the basic functionalities of arrays and hashes, for example to extract the values of my response you can use:

$response<object_you_want_of_json><other_nested_object>[1]<the_last_level>

Here the number [1] are a nested list inside a hash, and the properties are the same. Welcome to the raku community !!!

like image 5
metagib Avatar answered Oct 17 '22 22:10

metagib