Posting JSON data to another host

26 views
Skip to first unread message

Zed

unread,
Aug 9, 2015, 5:36:12 AM8/9/15
to Ruby on Rails: Talk

Hello Team,

 

I have a rails web application and have to post a json to the host below. 

 

Presently i am struggling.

 

Can you please help us a with a sample rails controller method to push this below notification.

 

POST /push?api_key=31fbfefcd4baa21221545c1233cf2631d9e3eca7f6e76b6c486144c115002de0 HTTP/1.1

Host: https://pushy.me

Content-Type: application/json

Connection: close

Content-Length: 82

 

{"registration_ids":["d62d6163f916d15eed601b"], "data":{"message":"Hello World!"}}



Frederick Cheung

unread,
Aug 10, 2015, 4:35:58 AM8/10/15
to Ruby on Rails: Talk


On Sunday, August 9, 2015 at 10:36:12 AM UTC+1, Zed wrote:

Hello Team,

 

I have a rails web application and have to post a json to the host below. 

 

Presently i am struggling.

 

Can you please help us a with a sample rails controller method to push this below notification.

 


net/http is a rather confusing bit of ruby.  You may find using a higher level client such as rest client or faraday makes things easier but something like the below should do it

req = Net::HTTP::Post.new(uri)
req.body ='...'
req.content_type = 'application.json'

res = Net::HTTP.start(uri.hostname, uri.port) {|http|
  http.request(req)
}

Fred
Reply all
Reply to author
Forward
0 new messages