Files
nairobi/doc/api/examples/ruby/example_1.rb
Javi Martín 56e42f209d Fix API examples
We were getting many errors when trying to run them, from uninitialized
constant `HTTP` to undefined method `headers`.

We might move these examples to the documentation repository in the
future, but we need to look for possible side-effects first.
2023-03-01 17:25:21 +01:00

30 lines
631 B
Ruby

require "net/http"
API_ENDPOINT = "https://demo.consulproject.org/graphql".freeze
def make_request(query_string)
uri = URI(API_ENDPOINT)
uri.query = URI.encode_www_form(query: query_string.delete("\n").delete(" "))
request = Net::HTTP::Get.new(uri)
request[:accept] = "application/json"
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https|
https.request(request)
end
end
query = <<-GRAPHQL
{
proposal(id: 1) {
id,
title,
public_created_at
}
}
GRAPHQL
response = make_request(query)
puts "Response code: #{response.code}"
puts "Response body: #{response.body}"