0

i'm trying to learn simple stuff a make HTTP request to rails app. the problem is, when i try to make HTTP post request to my rails app, code is :

uri = URI.parse("http://localhost:4000/posts")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data( :post=> {"name" => "My query", "title" => "50"} )
response = http.request(request)

when i look into console for incoming post request, the output is:

Started POST "/posts" for 127.0.0.1 at 2015-02-02 16:14:12 +0100
Processing by PostsController#create as */*
  Parameters: {"post"=>"{\"name\"=>\"My query\", \"title\"=>\"50\"}"}
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `permit' for "{\"name\"=>\"My query\", \"title\"=>\"50\"}":String):
  app/controllers/posts_controller.rb:72:in `post_params'
  app/controllers/posts_controller.rb:27:in `create'

Look closely on parameters. Why does ist add additional quotation marks? how to fix this problem?

1 Answers1

0

Because you are sending a stringified JSON object. I don't know what set_form_data is doing but you should be converting your request .to_json. Look at this so question for reference here.

Community
  • 1
  • 1
Yeysides
  • 1,262
  • 1
  • 17
  • 27