2

Snippet from the test:

my $request = HTTP::Request->new( POST => 'http://192.168.5.130:3000/user' );
$request->content_type('application/json');
$request->content( $query_string );
my $result = $ua->request( $request );

Snippet from the ajax call:

$.ajax({
    url: 'http://192.168.5.130:3000/user',
    type: 'POST',
    cache: false,
    async: true,
    dataType: 'json',
    timeout: 5000,
    data: $("#create-user-form").serializeObject(),
    error: function(jqXHR, textStatus, errorThrown){
    console.log("jQuery ajax error....");
},
); 

In the controller is the route handler (using Catalyst::Controller::Rest)

sub user_POST {
        my ($self, $c, $args) = @_;
    warn Dumper( $c );

...
 }

The problem I'm having is, when I make the call from the test the content is set in the Catalyst object in a hash with a key called 'data' which can be accessed be calling $c->req->data.

However, when the ajax call is made from a webpage, the data is held in the Catalyst object in a hash with a key called 'parameters' which is accessed by calling $c->req->parameters.

Anyone know why this is happening and or what to do to work around it?

Vimal
  • 1,266
  • 1
  • 9
  • 16
jmcneirney
  • 1,374
  • 8
  • 23

2 Answers2

0

Catalyst stores input in two different places. Query string parameters are made available in

$c->request->query_params

and body parameters are made available in

$c->request->body_params

To get all input, use

$c->request->params

From the documentation of the Catalyst::Controller::REST module:

The HTTP POST, PUT, and OPTIONS methods will all automatically deserialize the
contents of $c->request->body into the $c->request->data hashref
Mauritz Hansen
  • 4,674
  • 3
  • 29
  • 34
0

I'm not completely sure, but I would suspect that jQuery is appending the data you are sending in the Ajax request to the GET query string. Part of my doubt is that I do not know what .serializeObject() does in your Ajax snippet.

You might check the documentation regarding the data and processData options and see if that helps.

Here are the relevant sections:

data Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

...

processData By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

zostay
  • 3,985
  • 21
  • 30