LoginSignup
0
0

More than 3 years have passed since last update.

api リクエストレスポンス

Posted at
全体の流れ

require 'net/http'
require 'uri'
require 'json'


uri = URI.parse('http://localhost:3000/products/1')


ここでリクエストを出してる
ここは違和感だった。getメソッド使ってるのに、responseの変数を使ってる

response = Net::HTTP.get(uri)

result = JSON.parse(response)

p result

resultの結果

{"id"=>1, "name"=>"aa", "price"=>"1000", "integer"=>"1", "detail"=>"dsa", "image"=>"a", "created_at"=>"2019-05-08T15:29:33.411Z", "updated_at"=>"2019-05-08T15:29:33.411Z"}

responseの結果
json型になっていないみたい

JSON.parseで成形される

"{\"id\":1,\"name\":\"aa\",\"price\":\"1000\",\"integer\":\"1\",\"detail\":\"dsa\",\"image\":\"a\",\"created_at\":\"2019-05-08T15:29:33.411Z\",\"updated_at\":\"2019-05-08T15:29:33.411Z\"}"

値の撮り方

result["price"]
→ 1000

メモ

  • URI

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