John O
John O

Reputation: 5433

WWW::Mechanize doesn't like POSTing json

I have the following code:

my $j = encode_json { "arguments" => { "peer-port" => "4444" }, "method" => "session-set", };
$mech->get('http://192.168.1.10:9091');
my $req = HTTP::Request->new( 'POST', 'http://192.168.1.10:9091/transmission/rpc' );
$req->header( 'Content-Type' => 'application/json' );
$req->content($j);
$mech->request($req);

When it runs, I get the following error:

Error POSTing http://192.168.1.10:9091/transmission/rpc: Conflict at ./pia.pl line 48.

I am unable to find anything on this specific error, nor am I able to find anything in WWW::Mechanize's documentation (or HTTP::Request's) that sheds any light on it. The script has no problem doing proper form submissions, it only fails on this one (where the target http server only accepts ajax/json requests apparently).

Upvotes: 3

Views: 1937

Answers (2)

gangabass
gangabass

Reputation: 10666

You don't need to create an HTTP::Request object explicitly.

my $j = encode_json { "arguments" => { "peer-port" => "4444" }, "method" => "session-set", };
$mech->get('http://192.168.1.10:9091');
$mech->post("http://192.168.1.10:9091/transmission/rpc",
    'Content-Type' => 'application/json', Content => $j);

Upvotes: 4

Steffen Ullrich
Steffen Ullrich

Reputation: 123375

"Conflict" is a response from the server and you should check the details of the response. From RFC2616:

10.4.10 409 Conflict

The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough information for the user to recognize the source of the conflict. ...

So check the full response you get ($mech->content) to find out why a conflict occurred. If this does not help check the logs on the server side or consult the documentation of the server side API.

In your specific case it might be, that you need to add an X-Transmission-Session-Id header to your request, see https://forum.transmissionbt.com/viewtopic.php?f=8&t=8393 for more information.

Upvotes: 5

Related Questions