简体   繁体   中英

How to fire Delete method in HTTP request using Net::HTTP class in rails 4

I am working with google calendar in rails app. I have fetch all the events and now I want to delete the event from rails app. for that I get calendar.events.delete method from google calendar api. But I am not aware hot to fire delete request in http. I have tried all these answers From Google APIs Explorer I could successfully delete the specific event from my google calendar.

When I execute in google apis explorer by providing required details I got this url for delete the event:

DELETE https://www.googleapis.com/calendar/v3/calendars/{MY_CALENDAR_ID}%40gmail.com/events/mycalendar'seventID?key={MY_API_KEY}

For this I tried:

url = URI(url_path)
request = Net::HTTP.new(url)
request.delete

and

  uri = URI.parse(url_path)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Delete.new(uri.request_uri) 
  http.request(request)
 => #Error : end of file reached

I had also look this class library: Net::HTTP::Delete but here Delete is a class not method.

This way I am getting calendar's all events:

    source = 'https://www.googleapis.com/calendar/v3/calendars/my_calendar_ido%40gmail.com/events?key={MY_KEY}'
    gcalendar_resp = Net::HTTP.get_response(URI.parse(source))
    data = gcalendar_resp.body
    result = JSON.parse(data)

Can any one help me How can I fire delete request?

Try:

Net::HTTP.new('www.server.com').delete('/path')

Or:

uri = URI('url')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Delete.new(uri.path)
res = http.request(req)
puts "deleted #{res}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM