Qiita APIでQuine投稿(golang)


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

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


```go: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 = `
```