1
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 5 years have passed since last update.

Qiita APIでQuine投稿(golang)

Last updated at Posted at 2019-09-16

Quine投稿についてはこちら

この記事のAPIを実行したコード

quineRequest.go
package main

import (
    "bytes"
    "fmt"
    "net/http"
    "io/ioutil"
    "encoding/json"
)

type QiitaRequest struct {
  Private bool      "json:\"private\""
  Title string      "json:\"title\""
  Tags []QiitaTag   "json:\"tags\""
  Body string       "json:\"body\""
}

type QiitaTag struct {
  Name string       "json:\"name\""
  Versions []string "json:\"versions\""
}

func main() {
    authorization_token := "Bearer 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcd"

    l := string(10) // 改行
    b := string(96) // バッククォート
    
    tags := [] QiitaTag{ {"Qiita", [] string{}}, {"Go", [] string{}}, {"QiitaAPI", [] string{}}, {"quine投稿", [] string{}}, {"quine", [] string{}} }
    requestJson := QiitaRequest{
      Private: false,
      Title: "Qiita APIでQuine投稿(golang)",
      Tags: tags,
      Body: string(itemBody) + l + l + b + b + b + "go:quineRequest.go" + l + string(code) + b + string(itemBody) + b + l + l + "const code = " + b + string(code) + b + l + b + b + b,
    }
    jsonString, err := json.Marshal(requestJson)
    if err != nil {
        panic(err)
    }

    req, err := http.NewRequest(
//        "POST",
//        "https://qiita.com/api/v2/items",
        "PATCH",
        "https://qiita.com/api/v2/items/cf3347901c896c68266e",
        bytes.NewBuffer([]byte(string(jsonString))),
    )
    if err != nil {
        panic(err)
    }

    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Authorization", authorization_token)

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    fmt.Println("response Status:", resp.Status)
    fmt.Println("response Headers:", resp.Header)
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response Body:", string(body))
}

const itemBody = `
Quine投稿については[こちら](https://qiita.com/j5c8k6m8/items/bc2324ef036729da1a28)

# この記事のAPIを実行したコード
`

const code = `package main

import (
    "bytes"
    "fmt"
    "net/http"
    "io/ioutil"
    "encoding/json"
)

type QiitaRequest struct {
  Private bool      "json:\"private\""
  Title string      "json:\"title\""
  Tags []QiitaTag   "json:\"tags\""
  Body string       "json:\"body\""
}

type QiitaTag struct {
  Name string       "json:\"name\""
  Versions []string "json:\"versions\""
}

func main() {
    authorization_token := "Bearer 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcd"

    l := string(10) // 改行
    b := string(96) // バッククォート
    
    tags := [] QiitaTag{ {"Qiita", [] string{}}, {"Go", [] string{}}, {"QiitaAPI", [] string{}}, {"quine投稿", [] string{}}, {"quine", [] string{}} }
    requestJson := QiitaRequest{
      Private: false,
      Title: "Qiita APIでQuine投稿(golang)",
      Tags: tags,
      Body: string(itemBody) + l + l + b + b + b + "go:quineRequest.go" + l + string(code) + b + string(itemBody) + b + l + l + "const code = " + b + string(code) + b + l + b + b + b,
    }
    jsonString, err := json.Marshal(requestJson)
    if err != nil {
        panic(err)
    }

    req, err := http.NewRequest(
//        "POST",
//        "https://qiita.com/api/v2/items",
        "PATCH",
        "https://qiita.com/api/v2/items/cf3347901c896c68266e",
        bytes.NewBuffer([]byte(string(jsonString))),
    )
    if err != nil {
        panic(err)
    }

    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Authorization", authorization_token)

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    fmt.Println("response Status:", resp.Status)
    fmt.Println("response Headers:", resp.Header)
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response Body:", string(body))
}

const itemBody = `
1
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
1
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?