How to post request body with Mojo::UserAgent?

89 views
Skip to first unread message

Yuichi Tsunoda

unread,
Oct 27, 2023, 5:15:52 PM10/27/23
to Mojolicious
I tried below code. But the request body are missing.

my $ua = Mojo::UserAgent->new;

my $url = "https://postman-echo.com/post";

my $tx = $ua->post(

    $url => {

        'Authorization' => 'Basic xxxxxxxxxxxxxx',

        'Content-Type' => 'application/x-www-form-urlencoded'

    } => form => {

        'grant_type' => 'authorization_code',

        'code' => $code,

        'xxx' => '777'

    }

);



Here is a result. I expected value at "form" variables.

'{

"args": {},

"data": "",

"files": {},

"form": {},

"headers": {

"x-forwarded-proto": "https",

"x-forwarded-port": "443",

"host": "postman-echo.com",

"x-amzn-trace-id": "Root=1-653c253d-2bee01d92fa16ed279d4b219",

"content-type": "application/x-www-form-urlencoded",

"authorization": "Basic xxxxxxxxxxxxxx"

},

"json": null,

"url": "https://postman-echo.com/post"

}';


Anyone know the reason?



With regards,



jay m

unread,
Oct 29, 2023, 10:58:41 AM10/29/23
to Mojolicious
hi,
your code works for me with Mojolicious 8.12 -- i got the expected response back from postman-echo.com:

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "code": "whatever",
    "grant_type": "authorization_code",
    "xxx": "777"

  },
  "headers": {
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "host": "postman-echo.com",
    "x-amzn-trace-id": "Root=1-653e7226-05cb12432a6ed6e652b6afe2",
    "content-length": "51",
    "user-agent": "Mojolicious (Perl)",
    "authorization": "Basic xxxxxxxxxxxxxx",
    "accept-encoding": "gzip",
    "content-type": "application/x-www-form-urlencoded"
  },
  "json": {
    "code": "whatever",
    "grant_type": "authorization_code",
    "xxx": "777"
  },
  "url": "https://postman-echo.com/post"
}';

maybe you are using a VERY old version of Mojo::UserAgent that has the post_form( $url => $data) method?
jay
Reply all
Reply to author
Forward
0 new messages