[Rails] How to connect to an external API using HTTP Client (I tried connecting to Qiita API)

Introduction

Use gem's HTTP Client when connecting to an external API with rails. This time, as an example, I will connect to Qiita's API and get the article list.

Install HTTP Client

Gemfile


gem 'httpclient'

Terminal


$ bundle install

Added to route.rb

The endpoint is / api / qiita.

routes.rb


Rails.application.routes.draw do 
  namespace :api do 
    get '/qiita' to: 'qiita#index'
  end
end

Connect to Qiita API (Controller)

First, the most basic form. (Get request without header or query)

controllers/api/qiita_controller.rb


class Api::QiitaController < ApplicationController
  #Call HTTPClient
  require 'httpclient'

  def index
    url = "https://qiita.com/api/v2/items"  #Set URL
    client = HTTPClient.new                 #Create an instance
    response = client.get(url)              #Get request
    render json: JSON.parse(response.body)  #Parse and display the result in json
  end
end

When you access http: // localhost: 3000 / api / qiita, a list of data will be returned in json like this. It's very hard to see because it's not shaped.

スクリーンショット 2020-09-27 13.39.51.png

Next is the case of specifying header or query.

controllers/api/qiita_controller.rb


class Api::QiitaController < ApplicationController
  #Call HTTPClient
  require 'httpclient'

  def index
    url = "https://qiita.com/api/v2/items"
    header = { Authorization: "Bearer xxxxx" } #Example)In the header"Bearer xxxxx"Grant
    query = { page: 1, per_page: 20 }          #Example)1st page, query to increase the number of data acquisitions per page to 20
    client = HTTPClient.new
    response = client.get(url, header: header, query: query) #Specify header and query
    render json: JSON.parse(response.body)
  end
end

At the end

This time, I explained only get request, but HTTP Client can send other requests such as Post, so please check it.

For details of Qiita API, refer to the following. Qiita API v2 documentation - Qiita:Developer Overview of Qiita API v2 (unofficial)

Recommended Posts

[Rails] How to connect to an external API using HTTP Client (I tried connecting to Qiita API)
Rails6 I tried to introduce Docker to an existing application
I tried to build an environment using Docker (beginner)
[RSpec] Use WebMock to test logic using an external API
I tried to introduce UI animation to Pokedex using Poké API
I tried using Hotwire to make Rails 6.1 scaffold a SPA
[Rails] I tried to implement "Like function" using rails and js
[Rails] How to convert the URI of the image sent by http to https when using Twitter API
02. I made an API to connect to MySQL (MyBatis) from Spring Boot
I tried connecting to MySQL using JDBC Template with Spring MVC
I tried to build an http2 development environment with Eclipse + Tomcat
Correspondence when HTTP status code 302 occurs when connecting to an external URL
How to insert an external library
I tried to understand how the rails method "redirect_to" is defined
I tried to build a simple application using Dockder + Rails Scaffold
I tried using Java8 Stream API
After learning Progate, I tried to make an SNS application using Rails in the local environment
I tried to understand how the rails method "link_to" is defined
I tried to make it an arbitrary URL using routing nesting
How to return Rails API mode to Rails
Rails Tutorial Extension: I tried to create an RSS feed function
Rails API mode I tried to implement the keyword multiple search function using arrays and iterative processing.
I tried using Elasticsearch API in Java
I tried to introduce CircleCI 2.0 to Rails app
[Rails] How to upload images using Carrierwave
I tried to summarize the Stream API
How to implement image posting using rails
[Rails] How to handle data using enum
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
I tried barcode scanning using Rails + React + QuaggaJS
[Rails] How to create a graph using lazy_high_charts
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
The code I used to connect Rails 3 to PostgreSQL 10
I made an API client for Nature Remo
[API] I tried using the zip code search API
[Rails] How to upload multiple images using Carrierwave
Rails6.0 ~ How to create an eco-friendly development environment
How to build API with GraphQL and Rails
I tried to develop an application in 2 languages
[Rails] How to build an environment with Docker
I tried to implement a server using Netty
[Java] How to operate List using Stream API
I tried to check the operation of http request (Put) with Talented API Tester
How to request by passing an array to the query with HTTP Client of Ruby
How to push an app developed with Rails to Github
I tried using Google Cloud Vision API in Java
How to make an oleore generator using swagger codegen
I tried to operate SQS using AWS Java SDK
How to make an almost static page with rails
I started MySQL 5.7 with docker-compose and tried to connect
[Rails] I don't know how to use the model ...
I tried to draw animation with Blazor + canvas API
[Java] I tried to implement Yahoo API product search
I tried using an extended for statement in Java
How to write an external reference key in FactoryBot
Rails learning How to implement search function using ActiveModel
[Rails] How to display an image in the view
[Rails] How to install a decorator using gem draper
I want to connect to Heroku MySQL from a client
[Ruby On Rails] How to update the calculated result to an integer type column using update_column