Generic_User_ID
Generic_User_ID

Reputation: 1217

Why won't this POST request send a JSON string to the server?

I'm using Sinatra. I've got a button that sends a JSON-formatted string to the server by making a POST request. Only problem is, the JSON string isn't getting to the server.

Here's index.erb:

<input id="post-button" type="button" value="Send POST request">

Here's script.js:

window.onload = function(){
  var btn = document.getElementById('post-button');
  btn.onclick = function(){
    var req = new XMLHttpRequest();
    var reqObj = {
      name: 'Joe',
      age: '100'
    };
    var reqJSON = JSON.stringify(reqObj);

    req.open('POST','/post_target',true);
    req.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
    req.send(reqJSON);
  }
}

And finally, main.rb:

get '/' do 
    erb :index
end

post '/post_target' do 
    p params
end

When I click the button and check Firebug, I see that the browser sent a POST request. But when I check the Ruby console, it prints {} for params. If it worked right, I guess params would show a JSON object in string form, along with whatever else it would show.

I'm running Sinatra on my machine, at address http://localhost:4567.

What am I doing wrong? If I need to do the request with jQuery, I can do that, but can it be done with "vanilla" JavaScript?

Upvotes: 1

Views: 542

Answers (3)

Sir l33tname
Sir l33tname

Reputation: 4330

Alex Hill is right. And the the post he mentioned show one way to solve it.

An other way is to use rack/contrib:

require it:

require 'rack'
require 'rack/contrib'

add the middleware

use Rack::PostBodyContentTypeParser

source: http://jaywiggins.com/2010/03/using-rack-middleware-to-parse-json/

Upvotes: 1

B Seven
B Seven

Reputation: 45941

You need to parse the JSON body:

parsed_params = JSON.parse( request.body.read, symbolize_names:true )

Upvotes: 1

Alex Hill
Alex Hill

Reputation: 711

"params" is most likely looking for URL encoded query string values. You want the Body content of the post, not the params.

See How to parse JSON request body in Sinatra just once and expose it to all routes?

Upvotes: 1

Related Questions