0

I need to add a header to my hash that I convert to JSON.

In my controller I have:

render json: @rates

With @rates being a hash that looks like this:

{:rates=>[{:service_name=>"Standard", :service_code=>"FU", 
:total_price=>"1100", :currency=>"USD", :min_delivery_date=>"2016-03-11 
08:00:00 +0000", :max_delivery_date=>"2016-03-16 06:59:59 +0000"}, 
{:service_name=>"Priority", :service_code=>"FU", :total_price=>"2300", 
:currency=>"USD", :min_delivery_date=>"2016-03-08 08:00:00 +0000", 
:max_delivery_date=>"2016-03-09 07:59:59 +0000"}, 
{:service_name=>"Expedited", :service_code=>"FU", :total_price=>"1420", 
:currency=>"USD", :min_delivery_date=>"2016-03-09 08:00:00 +0000", 
:max_delivery_date=>"2016-03-10 07:59:59 +0000"}]}

The JSON format is perfect, but I believe I need a header for it for the API to get the rates from me. I saw "Render JSON with header", but I don't get it.

I need to add:

Content-Type: application/json

Can I just add this to my hash as another key/value pair? Or do I have to do some special header value?

Community
  • 1
  • 1
ToddT
  • 3,084
  • 4
  • 39
  • 83

1 Answers1

0

You can try adding the content_type option.

render json: @rates, content_type: "application/json"
SacWebDeveloper
  • 2,563
  • 2
  • 18
  • 15
  • tried but no luck.. I wish there was some service that I could post my JSON too and the API could hit it and see if it right.. Anyone know of anything out there? – ToddT Mar 07 '16 at 19:25
  • I found it!! this is the service I've been looking for: [Hurl.it](https://www.hurl.it/) – ToddT Mar 07 '16 at 21:37
  • But Hurl.it requires your application to be publicly accessible. If you are developing JSON endpoints and testing locally, you can just use Postman. http://www.getpostman.com/ – SacWebDeveloper Mar 07 '16 at 22:08
  • oh man! that's even better.. Thank you! I just need to be able to see what the heck is going on.. I am developing on Cloud9 and I'm starting to think it has something to do with that.. – ToddT Mar 08 '16 at 14:19