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

  • SEARCH
  • Home
  • 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 9141977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:43:00+00:00 2026-06-17T09:43:00+00:00

I am trying to create a simple crud interface that spits out some json.

  • 0

I am trying to create a simple crud interface that spits out some json. (api)

I’m using the inspiring spree code base in order to make it, by now, there is no token to make it secure

Please see the code below:

helper/api/api_helper.rb

module Api
  module ApiHelper
    def required_fields_for(model)
      required_fields = model._validators.select do |field, validations|
        validations.any? { |v| v.is_a?(ActiveModel::Validations::PresenceValidator) }
      end.map(&:first)
    end

    def product_attributes
      [:id, :name]
    end
  end
end

views/api/products/new.rabl

object false
node(:attributes) { [*product_attributes] }
node(:required_attributes) { required_fields_for(Product) }

controllers/api/products_controller.rb

def new
end

def create
  @product = Product.new(params[:product])
  if @product.save
    respond_with(@product, :status => 201, :default_template => :show)
  else
    invalid_resource!(@product)
  end
end

When I do a post request via curl the method returns a 422 Unprocessable Entity error due to the fact that the product name should not be empty (model validation). As you could see the name is correctly set!!

Didn’t find what’s wrong with this (serialization?) thank you for your thought

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"attributes":{"name":"test"}}'  http://localhost/api/brands

* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> POST /api/products HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3
> Host: localhost
> Accept: application/json
> Content-type: application/json
> Content-Length: 31
> 
< HTTP/1.1 422 Unprocessable Entity
< Content-Type: application/json; charset=utf-8
< X-Meta-Request-Version: 0.2.0
< X-UA-Compatible: IE=Edge
< Cache-Control: no-cache
< X-Request-Id: e32527947ed4ae51052b4d65d9d4539f
< X-Runtime: 0.042655
< Connection: close
< 
* Closing connection #0
{"errors":{"name":["doit \u00eatre rempli(e)"]}}
  • 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-06-17T09:43:01+00:00Added an answer on June 17, 2026 at 9:43 am

    The JSON payload is expected to be nested under the model’s name. Try this:

    -d ' {"product":{"name":"test"}}'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create simple CRUD application. I have a partial view that
I am trying to learn Hibernate and I could create some simple CRUD operation
I was trying to create simple program that would perform some trivial operation with
I am a newbie trying to create a simple (in theory) CRUD application using
I'm trying to create simple PayPal Pay API operation, when I run this code
I'm trying to create simple script that subscribes a user to a company calendar.
Im trying to create a simple pan and zoom app using silverlight 4, but
I am trying to create a simple about page that uses JQuery click to
I am trying to create a simple program using Gstreamer to take the input
I'm trying to create a simple web page that resembles the following: I've got

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.