Over a million developers have joined DZone.
Platinum Partner

Making HTTPs Requests in Ruby & JavaScript w/ Node.js

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix. Download this Forrester Report to gain a better understanding of the low-code platform market and how to make a strategic platform selection you won’t regret.

Making HTTPS requests seems oddly difficult. Why can't an HTTP library just figure things out for me like curl does? It mostly can...

Ruby

You have to breakup the URL into a host, port, and path, then make a request with use_ssl.
 

    $ irb
     
    require 'net/http'
    uri = URI::parse("https://artsy.net/api/v1/system/up")
    Net::HTTP.start uri.host, uri.port, use_ssl: (uri.scheme == "https") do |http|
      request = Net::HTTP::Get.new(uri.path)
      response = http.request request
      puts response.body
    end

Node.js

A little simpler. Superagent, also known as “ajax with less suck,” can take care of everything.

    $ npm install -g superagent
     
    $ coffee
     
    http = require('superagent')
    http.get('https://artsy.net/api/v1/system/up').end((err, res) -> console.log res.body)




The Web Dev Zone is brought to you in partnership with Mendix. Better understand the aPaaS landscape and how the right platform can accelerate your software delivery cadence and capacity with the Gartner 2015 Magic Quadrant for Enterprise Application Platform as a Service.

Topics:

Published at DZone with permission of Daniel Doubrovkine .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}