Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send an empty array through a PUT request via HTTParty?

I have a Photo class that has a "name" attribute and a "tags" attribute. My goal is to implemented an update function in Rails that replaces the photo's tags with whatever was inputted. For example, if I try to PUT a JSON object that has "tags" set to [], I want any tags to be cleared from the photo.

However, when I submit an empty array through HTTParty as one of the body parameters, I believe that HTTParty is translating [] into nil. Therefore the photos#update endpoint on my Rails backend receives nothing for the parameter "tags". I am looking for a way for HTTParty to not convert [] into nil because I lose the ability to remove tags from the photo.

like image 338
Donald Huh Avatar asked Mar 14 '13 01:03

Donald Huh


1 Answers

This is a bug/feature in Rails 4, you can read more about the drama at: https://github.com/rails/rails/issues/13420

Your options include:

  • One-off hack similar to @mkk's answer
  • Upgrade to Rails 5
  • Disable deep_munge everywhere, and deal with the consequences of that:

>

# config/application.rb
config.action_dispatch.perform_deep_munge = false
like image 161
Meekohi Avatar answered Oct 20 '22 09:10

Meekohi