Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:31:51+00:00 2026-05-24T09:31:51+00:00

I have a Rails controller with a create method, and I want to POST

  • 0

I have a Rails controller with a create method, and I want to POST to it from a Ruby script. When I hit this method from a browser-based POST, the params hash is set appropriately, but when I hit it from my script, the hash is empty. I’ve already disabled protect_from_forgery, and the method does get called; it just has an empty params. Here is the controller method:

def create
  logger.info "GOT: #{request.body.read}"
  logger.info "PARAMS: #{params.inspect}"
  az = AreaZip.new(params[:area_zip])
  if az.save
    respond_to { |format|
      format.js { render :json => {:id => az.id} }
    }
  else
    logger.warn "FAILED creating AreaZip: #{az.errors.inspect}"
    respond_to { |format|
      format.js { render :json => {:errors => az.errors} }
    }
  end
end

Here is the code that does the posting:

def self.post(url, path, params={})
  url = "#{url}#{path}"
  uri = URI.parse(url)
  headers = {
    'Content-Type' => 'text/plain; charset=utf-8',
    'Authorization' => "Basic #{Base64.encode64('censored:censored').strip}",
  }

  http = Net::HTTP.new(uri.host, uri.port)
  post_data = params.map {|k,v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}" }.join("&")
  resp, data = http.post(uri.path, post_data, headers)
  return JSON.parse(data)
end   

Here is what I see if I post to nc -l -p 12345:

POST /admin/area_zips HTTP/1.1
Accept: */*
Connection: close
Content-Type: text/plain; charset=utf-8
Authorization: Basic sdfnjjsadf[censored]jklsdfsjdfkj
Content-Length: 54
Host: localhost:12345

area_zip%5Bzip%5D=15068-4838&area_zip%5Barea_id%5D=334

If I post to my Rails app, my log shows this:

Started POST "/admin/area_zips" for 127.0.0.1 at Fri Jul 29 09:45:05 -0400 2011
  Processing by Admin::AreaZipsController#create as */*
Asking for HTTP basic authentication to reach admin page
GOT: area_zip%5Bzip%5D=15068-6851&area_zip%5Barea_id%5D=334
PARAMS: {"action"=>"create", "controller"=>"admin/area_zips"}
  SQL (0.2ms)  BEGIN
  SQL (0.2ms)  ROLLBACK
FAILED creating AreaZip: #<OrderedHash {:area=>["can't be blank"], :zip=>["can't be blank"]}>
Completed 200 OK in 70ms (Views: 41.4ms | ActiveRecord: 1.2ms)

So as you can see, Rails gets the POSTed data, but it’s not using it to set up params. Any idea why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:31:51+00:00Added an answer on May 24, 2026 at 9:31 am

    Okay, I solved this. Turns out the problem was the Content-Type header. I need to set it to application/x-www-form-urlencoded. Actually I thought I was doing this, but apparently not. That old Content-Type value was a copy-paste error, leftover from the code in my get method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make my rails controller more flexible and try to create some
This is the code I have at the end a the create method in
I have a problem concerning the build method in rails 3. I want to
I have controller vegetable . It has standart new and create methods. I want
I have a Rails application that unfortunately after a request to a controller, has
Using Rails v2.1, lets say you have an action for a controller that is
Ruby on Rails controllers will automatically convert parameters to an array if they have
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a Rails app with a Route resource, and a Route controller (not
Environment: Rails 3.0.1, MySQL I have a Users table, and I want to search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.