'How to send a POST request with after create or update of a user (RAILS)
This is my first time working with Requests and i'm a little confused, any guidance would be greatly appreciated.
Task: after a user is created i would like to send that user's information (name, email, etc) to another site that we are integrating with. The goal is when a user leaves our site and goes to the next site (we're also setting up SSO) they would see the same information.
Here is what I put in the Model:
after_validation :send_new_user_to_hivebrite, on: [ :create, :update ]
def send_new_user_to_hivebrite
payload = {user:{email:Email.find_by(person_id:self.id).address,sub_network_ids:[0],firstname:self.given_name,lastname: self.family_name,is_active:true}}
begin
response = RestClient::Request.execute(
method: :post,
url: 'https://mysite.us.hivebrite.com/api/admin/v1/users',
payload: payload.to_json,
headers: {content_type: :json, accept: :json, Authorization: 'my_token'}
)
self.description = "Response code: #{response.code}"
rescue RestClient::ExceptionWithResponse => e
self.description = "It didn't work"
end
end
I tried doing the same thing but with a GET request (without the payload) and stored the response in self.description and it worked. so the authorization works and the url works. which means something is wrong with my payload right? am i formatting it wrong? this exact format worked in postman:
{
"user":
{
"email": "[email protected]",
"sub_network_ids": [ 0 ],
"firstname": "Moe",
"lastname": "Test",
"external_id": "test_person_id",
"sso_identifier": "test_identifier",
"is_active": true,
"headline": "Nothing to fear, POSTMAN is here",
"summary": "This is my test summary, you will only see it if this post request worked!"
}
}
I thought maybe it couldn't find the email because it was not created yet or something, so i tried updating an existing user and it still goes to rescue.
In rails C i did
payload = {user:{email:Email.find_by(person_id:Person.last.id).address,sub_network_ids:[20548],firstname:Person.last.given_name,lastname:Person.last.family_name,is_active:true}}
then i did
irb(main):016:0> payload.to_json
=> "{\"user\":{\"email\":\"[email protected]\",\"sub_network_ids\":[20548],\"firstname\":\"temp\",\"lastname\":\"tempsir\",\"is_active\":true}}"
I checked https://jsonlint.com/ to see if that is valid json and it was. sooo what am I doing wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
