0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Ruby】HTTP通信でPOSTリクエストを送る

Posted at

はじめに

Yahooのルビ振りAPIを使用するため、net/httpライブラリを使ってPOSTの実装を行った記録です。

実装例1

  • Net::HTTP#postでフォームの情報をサーバに送信する
require 'net/http'
require 'uri'
require 'json'

  def post
    app_id = "hogehogehogehogehoge"  #  Yahoo APIのClient ID(アプリケーションID)
    url = "https://jlp.yahooapis.jp/FuriganaService/V2/furigana"

    headers = {
      "Content-Type" => "application/json",
      "User-Agent" => "Yahoo AppID: " + app_id,
    }

    param_dic = {
      "id": request_id,
      "jsonrpc": "2.0",
      "method": "jlp.furiganaservice.furigana",
      "params": {
        "q": "hoge",  # 入力された文字列
        "grade": 1
      }
    }

    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

    response = http.post(uri.path, param_dic.to_json, headers)
  end

解説

uri = URI.parse(url)

  • 与えられたURIから該当するURI::Genericのサブクラスのインスタンスを生成して返す
require 'uri'
uri = URI.parse("https://jlp.yahooapis.jp/FuriganaService/V2/furigana")
=> #<URI::HTTPS https://jlp.yahooapis.jp/FuriganaService/V2/furigana>
p uri.scheme
=> "https"
p uri.host
=> "jlp.yahooapis.jp"
p uri.port
=> 443
p uri.path
=> "/FuriganaService/V2/furigana"

Net::HTTP.new(uri.host, uri.port)

  • Net::HTTPオブジェクトを生成する
    • Net::HTTPはHTTPのクライアントのためのクラス

http.use_ssl = true

  • HTTPでSSL/TLSを使用する場合はtrueを設定する
  • デフォルトはfalse

response = http.post(uri.path, param_dic.to_json, headers)

  • post(path, data, header = nil, dest = nil) でサーバ上のpathにあるエンティティに対して文字列dataをPOSTで送る
  • 返り値はNet::HTTPResponseクラスのオブジェクト
    • Net::HTTPResponseはHTTP レスポンスを表現するクラス。 Net::HTTP クラスは実際には HTTPResponse のサブクラスを返す

実装例2(より細かく制御する)

  • Net::HTTPRequestクラス(HTTP リクエストを抽象化するクラス)のサブクラスであるNet::HTTP::Post(HTTP の POST リクエストを表すクラス)のリクエストオブジェクトを生成する
  • Net::HTTP#requestでリクエストをサーバに送信する
require 'net/http'
require 'uri'
require 'json'

  def post
    app_id = "hogehogehogehogehoge"  #  Yahoo APIのClient ID(アプリケーションID)
    url = "https://jlp.yahooapis.jp/FuriganaService/V2/furigana"

    headers = {
      "Content-Type" => "application/json",
      "User-Agent" => "Yahoo AppID: " + app_id,
    }

    param_dic = {
      "id": request_id,
      "jsonrpc": "2.0",
      "method": "jlp.furiganaservice.furigana",
      "params": {
        "q": "hoge",  # 入力された文字列
        "grade": 1
      }
    }

    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

+   request = Net::HTTP::Post.new(uri.path, headers) 
+   response = http.request(request)
-   response = http.post(uri.path, param_dic.to_json, headers) 
  end

解説

request = Net::HTTP::Post.new(uri.path, headers)

  • new(path, initheader = nil) でHTTP リクエストオブジェクトを生成する

response = http.request(request)

  • request(request, data = nil)でリクエストオブジェクトをサーバに送信する
  • 返り値はNet::HTTPResponseクラスのオブジェクト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?