您的位置:首页 > 其它

perl 模拟curl post请求

2017-01-17 19:45 357 查看
obtain auth token

curl -d "username=username@example.com&password=123456" https://cloud.seafile.com/api2/auth-token/ 
{"token": "24fd3c026886e3121b2ca630805ed425c272cb96"}

curl -d 说明:

-d/--data <data>
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit  button.  This  will
cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.  Compare to -F/--form.

-d/--data is the same as --data-ascii. To post data purely binary, you should instead use the --data-binary option. To URL-encode the value of a form field you may use --data-urlencode.

If  any  of  these  options  is  used  more than once on the same command line, the data pieces specified will be merged together with a separating &-symbol. Thus, using ’-d name=daniel -d
skill=lousy’ would generate a post chunk that looks like ’name=daniel&skill=lousy’.

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin.  The contents of the file must already  be
URL-encoded. Multiple files can also be specified. Posting data from a file named ’foobar’ would thus be done with --data @foobar.

(HTTP)发送指定的数据 以一个POST 请求到HTTP SERVER, 在浏览器中 当一个用户已经填写HTML 表单和按下提交按钮。

这个导致curl 来传递数据到server 使用 content-type application/x-www-form-urlencoded.

相比 -F/--form. -d/--data is the same as --data-ascii

post 数据纯2进值, 为了 URL-encode 表单字段的值 你可以使用--data-urlencode.

use  LWP::UserAgent;
use LWP;
use Encode;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Headers;
use HTTP::Response;
use Encode;
use URI::Escape;
use URI::URL;
use JSON;
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0");
my $cookie_jar = HTTP::Cookies->new(
file=>'lwp_cookies.txt',
autosave=>1,
ignore_discard=>1);
$ua->cookie_jar($cookie_jar);
my $token_url= 'http://127.0.0.1:8000/api2/auth-token/';

my $res = $ua->post($token_url,
{
'username'=>'015208@zjtlcb.com',
'password'=>'1234567'
});
print $res->content();
print "\n";
my $r= $res->content();
my $r=encode_utf8($r);
my $hash = decode_json($r);
print $hash->{"token"};



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: