Testing a REST API that receives params in json and returns data in json

Hey everyone,

This is a problem that has been bothering me for some time. I am
an API function that should receive data in json and response in json.
controller tests run fine(Since I abstract that the data gets there
decode from JSON and only the answer needs to be interpreted ).

I Also know that the function runs fine since I have used curl to test
with JSON arguments and it works perfectly.
(ex: curl -i --header “Accept: application/json” --header “Content-Type:
application/json” -d ‘{“test”:{“email”:“[email protected]”}}’

But obviously I would like to write request(feature) tests to test this
automatically and the way I see it they should work exactly like curl,
i.e., hit my service like it was an external call. That means that I
like to pass the arguments in JSON and receive an answer. I am pretty
since all the examples I can see people treat arguments as it was

My question is: I am following a wrong premise in wanting to send the
arguments and request as a JSON one since i will be testing that rails
works, because this is its responsibility? But I would like to see how
robust my code his to wrong arguments and would like to try with JSON.

something of this type:

it “should return an error if there is no correct email” do
params = {:subscription => {:email => “andre”}}

post “/magazine_subscriptions”, { ‘HTTP_ACCEPT’ => “application/json”,
‘Content-Type’ => ‘application/json’, ‘RAW_POST_DATA’ => params.to_json

Do you know how this is possible? and please let me know if you think I
testing it wrong.

all the best,


This may help you :

What you are asking for is a way to test your API from the perspective
of a
client ( like curl ) . I wanted to do the same thing in a project I
on, and I used Cucumber + Rack::Test + some JSON helper methods and I am
really happy with how it worked out. I’ve blogged about this here :
http://www.emilsoman.com/blog/2013/05/18/building-a-tested/ . I’ve set
an example Rails 4 app with tests here :
https://github.com/emilsoman/rails-4-api . Hope that helps.

Hi Emil,

I have also found that link and that only shows GET requests. Its a very
good example for those. But I need to make a pull request where I need
send data in JSON just like the example you posted here in cucumber, and
dont seem to find the way to do this in Rspec. at least I cant make it
receive the arguments in JSON.

all the best,


Did you try reading the Rspec docs? Also, this would help you:

Dheeraj K.

Hello there,

I have found ur post while googlin. Its very useful if you wanna use
cucumber. I dont like cucumber for many reasons and would like to use
to test this. do you know how to do it? If so can you help me?

thanks for the help though,

all the best,


Thanks everyone for your help,

I have found the answer here

It seems that the request parameters I was sending was not the correct

this worked

it “should return an error if there is no correct email” do
params = {:subscription => {:email => “andre”}}

post "/magazine_subscriptions", params.to_json, {'ACCEPT' =>

“application/json”, ‘CONTENT_TYPE’ => ‘application/json’}end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs