Notification Preferences API

Jump to solution
robotcars
Community Champion

I'm trying to update 2 notification preferences for users through the API with Ruby. I'm either having a problem understanding the API or maybe something is wrong.

 

1) I have masquerading privileges, Masquerading - Canvas LMS REST API Documentation, but I might be having trouble understanding the format of the query string to be sent. Notification Preferences - Canvas LMS REST API Documentation 

 

Specifically, what notification_preferences[<X>][frequency] is supposed to look like for:

{ "frequency": "immediately", "notification": "conversation_message", "category": "conversation_message" },

{ "frequency": "never", "notification": "added_to_conversation", "category": "added_to_conversation" },

 

I have been attempting to follow this post, and format my URI the same way.

https://community.canvaslms.com/message/57363-setting-default-user-notification-preferences-via-api#... 

 

2) I get 422 UNPROCESSABLE ENTITY, as my response.

The server understands the content type of the request entity (hence a 415 Unsupported Media Type status code is inappropriate), and the syntax of the request entity is correct (thus a 400 Bad Request status code is inappropriate) but was unable to process the contained instructions.

 

Here's my Ruby/HTTParty Class, URI and Query String below each attempt.

class CommChannelUpdater
     include HTTParty
     base_uri 'https://x.test.instructure.com/api/v1'
     headers 'Authorization' => 'Bearer AbraCaDAbRA', 'User-Agent' => 'ccsd-canvas-api'
     debug_output STDOUT

     def channel(user)
          self.class.get("/users/#{user}/communication_channels", :verify => true)
     end

     def update(user, channel)
          # attempt to set multiple preferences
          # self.class.post("/users/self/communication_channels/#{channel}/notification_preferences",
          #      :verify => true,
          #      :query => {
          #           :as_user_id => user,
          #           :notification_preferences => {
          #                :added_to_conversation => {:frequency => 'immediately'},
          #                :conversation_message => {:frequency => 'immediately'}
          #           }
          #      }
          # )
          # 422:https://x.test.instructure.com/api/v1/users/self/communication_channels/321/notification_preferences?as_user_id=123&notification_preferences[added_to_conversation][frequency]=immediately&notification_preferences[conversation_message][frequency]=immediately
           
          # self.class.post("/users/self/communication_channels/#{channel}/notification_preferences",
          #      :verify => true,
          #      :query => {
          #           :as_user_id => user,
          #           :notification_preferences => {
          #                :added_to_conversation => 'immediately',
          #                :conversation_message => 'immediately'
          #           }
          #      }
          # )
          # 422:https://x.test.instructure.com/api/v1/users/self/communication_channels/321/notification_preferences?as_user_id=123&notification_preferences[added_to_conversation]=immediately&notification_preferences[conversation_message]=immediately

          # attempt to set 1 at a time
          self.class.post("/users/self/communication_channels/#{channel}/notification_preferences/added_to_conversation", :verify => true, :query => {:as_user_id => user, :notification_preferences => {:frequency => 'immediately'}})
          # 422:https://x.test.instructure.com/api/v1/users/self/communication_channels/4968576/notification_preferences/added_to_conversation?as_user_id=4244756&notification_preferences[frequency]=immediately

     end
end‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

 

Any thoughts or assistance would be greatly appreciated!

1 Solution
pklove
Community Champion

I'm not familiar with Ruby, but it looks like you are doing a POST instead of a PUT.

View solution in original post