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?

More than 1 year has passed since last update.

Ruby | Qiita API で記事を更新する ( PATCHリクエスト )

Last updated at Posted at 2022-05-14


require 'net/https'
require 'uri'
require 'json'

uri = URI.parse("https://qiita.com/")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true


QIITA_API_TOKEN = 'xxxxxxxxxxxxxxxxxx'

# https://qiita.com/YumaInaura/items/yyyyyyyyyyyyyyyyyyy のような記事URLから id が分かる
qiita_item_id = 'yyyyyyyyyyyyyyyyyyy'

patch_url = "https://qiita.com/api/v2/items/#{qiita_item_id}"
patch_request_header = {
 'Content-Type' =>'application/json',
 "Authorization" => "Bearer #{QIITA_API_TOKEN}"
 }
patch_request = Net::HTTP::Patch.new(patch_url, patch_request_header)

# タイトルと記事本文は必須
patch_request.body = {
 'title' => 'Title',
 'body' => "AAA\nBBB\nUpdated at #{Time.now}"
 }.to_json

patch_response = http.request(patch_request)

puts patch_response.body

更新の例

image.png

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

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?