Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It doesn't seem possible to POST application/json data #214

Open
mscha opened this issue Aug 25, 2018 · 2 comments
Open

It doesn't seem possible to POST application/json data #214

mscha opened this issue Aug 25, 2018 · 2 comments
Labels

Comments

@mscha
Copy link

mscha commented Aug 25, 2018

I'm trying to create a POST request like:

POST /foo HTTP/1.1
Content-Type: application/json
Host: example.com

{"foo":1, "bar": 2}

As far as I can figure out, this is not currently possible.
If I set the Content-Type header of the request to 'application/json', add-form-data seems to do nothing at all.

@jonathanstowe
Copy link
Collaborator

jonathanstowe commented Aug 26, 2018

Sure it's possible, it's just not immediately obvious from the documentation :)

The easiest way is to create the HTTP::Request object with the POST helper in HTTP::Request::Common :

use HTTP::Request::Common;
use HTTP::UserAgent;


my $r = POST('http://example.com/foo', content => '{"foo":1, "bar": 2}', Content-Type => "application/json");

say $r.Str;

my $au = HTTP::UserAgent.new;

say $au.request($r).Str;

Which will give you a request like:

POST /foo HTTP/1.1
Host: example.com
Content-Type: application/json
Content-Length: 19

{"foo":1, "bar": 2}

In the general case if you want to create the HTTP::Request by hand, you would want to use add-content rather than add-form-data:

use HTTP::Request;
  

my $r = HTTP::Request.new(POST => 'http://example.com/foo');

$r.header.field(Content-Type => 'application/json');

$r.add-content: '{"foo":1, "bar": 2}';

say $r.Str;

I would however agree if you were to say that this might not be particularly well documented.

@Antisunny
Copy link

image

This is really a shit. The data is forcely encoded as application/x-www-form-urlencoded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants