![](/img/trans.png)
[英]How to POST form data in JSON format instead of JSON with key value pair using AJAX/jQuery?
[英]How to POST non key/value pair data with Perl and LWP
這個“問題”要求在這里對答案進行說明: 如何使用LWP發出JSON POST請求?
我沒有對評論發表評論的聲譽,並且認為發布我的問題作為答案是不合適的。
具體來說,我正在嘗試發布JSON數據(就像其他提問者一樣),而不是鍵值對。
為什么這樣做:
my $lwp = LWP::UserAgent->new;
my $req = HTTP::Request->new( 'POST', $uri );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );
my $response = $lwp->request( $req );
但這不是:
my $req= POST( $uri, $json); ### this works for key/value pairs
$req->header( 'Content-Type' => 'application/json' );
my $response = $lwp->request( $req);
...而且這也不是:
my $response = $lwp->request(POST $uri, ['Content-Type' => 'application/json'], $json);
我已經閱讀了HTTP :: Request :: Common和LWP :: Useragent的手冊,我認為我只是在看錯東西。
同樣,第一個示例運行良好,但我真的很想更好地理解這一點。
謝謝。
為什么要運作? 從文檔 :
POST $url
POST $url, Header => Value,...
POST $url, $form_ref, Header => Value,...
POST $url, Header => Value,..., Content => $form_ref
POST $url, Header => Value,..., Content => $content
你要
POST($uri, Content => $json)
除非這是更大的應用程序的一部分(否則可能還會),否則我建議使用Mojo :: UserAgent ,它具有執行此類操作的非常簡單的工具。
use strict;
use warnings;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
$ua->post( $uri, json => $json );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.