Perl6 http::useragent (3) JSON

Source: Internet
Author: User

If a URL requires the post data to be in JSON format, how do we send the data?

The first type:

Http::request

In the previous article, to send POST data, you can:

1 $ua %data)
2 $request. Add-form-data (%data)    $ua. Request ($request)

Here, either the first method or the second method, the%data sent inside will be automatically encoded.

JSON is also a string format, both of which require%data to be a hash, which means that both methods do not implement the sending of JSON.

Http::request there is actually another way, called:

Add-content

Some methods accept the arguments of a string, and we can commit the JSON in this way:

my $request = http::request.new (post=>'http://localhost/')

To add a JSON string:

>$request.content (Any)>%data=: User<root>,:p assword<root>{Password= root, user =Root}>$request. Add-content (To-json (%data)) Nil>$request.content{"Password":"Root","User":"Root" }>

I used the To-json (this method in the module Json::tiny) to convert a%data into a JSON string and then pass in $request.

Then you can ask for it:

My $html = $ua. Request ($request)

We can print out the request parameters to see if the JSON format string is actually sent:

>$html. Request.Strpost/http/1.1Host:localhostcontent-Length: +Connection:Close{ "Password":"Root","User":"Root" }>

As you can see, the string data is sent in JSON format.

We just send a JSON string on the line, my code above, just for convenience, to convert a%data into a string, in fact, we can define a JSON format string, and then add-content Add, the last to send, as follows:

Create a string first:

my $post _string ' {"Password": "Root", "user": "Root"} '

Add the Request object and ask:

$request. Add-content ($post _string) Nil$html$ua. Request (  $request)

The final print request parameter is correct:

>$html. Request.Strpost/http/1.1Host:localhostcontent-Length: theConnection:Close{"Password":"Root","User":"Root"}>

Can be seen, the same can be sent normally.

In addition to sending JSON requests with Http::request, PERL6 also has a module:

Www

This module can receive post data in string format, which is JSON:

Multi JPost ($urlwhere uri:d| STR:D, *%form); Multi JPost ($urlwhere uri:d| Str:d,%headers, *%form); Multi JPost ($urlwhere 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',% (Content-type ='Application/json'),: 72foo,:bar<?>;

Use it to send a request, like this:

' http://localhost ' %headers;

This is the JPost request, which automatically gets the JSON-formatted return value, and if you want to get a response request in a generic format, you can use its POST method.

The WWW module can be found at the following address:

Https://github.com/zoffixznet/perl6-WWW

We can look at its POST source code, in fact, it is also used http::request for encapsulation only:


Perl6 http::useragent (3) JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.