I want to convert the string to Json and give it a return value.
c.JSON(200, string(body))
"{\"message\":{\"@type\":\"response\",\"@service\":\"service.community.cafe\",\"@version\":\"1.0.0\",\"status\":\"200\",\"result\":{\"msg\":\"Success\",\"url\":\"aaabcd\",\"articleId\":211,\"articleUrl\":\"https://abcde.com/abc/211\"}}}"
// WriteResult Struct
type WriteResult struct {
Message int `form:"msg" json:"msg"`
URL string `form:"url" json:"url"`
ArticleID int `form:"articleId" json:"articleId"`
ArticleURL string `form:"articleUrl" json:"articleUrl"`
}
func writePost(c *gin.Context) {
var writeInfo WriteInfo
if err := c.ShouldBind(&writeInfo); err != nil {
fmt.Println("error : ", err)
}
url := "https://openapi.abcde.com/articles"
var bearer = "Bearer " + writeInfo.AccessToken
var bufs bytes.Buffer
form := url.Values{}
form.Add("subject", subject)
form.Add("content", content)
req, err := http.NewRequest("POST", url, strings.NewReader(form.Encode()))
req.Header.Add("Authorization", bearer)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
writeResult := new(WriteResult)
body, _ := ioutil.ReadAll(resp.Body)
c.JSON(200, string(body))
}
Is there a way to convert to Json and return it?
Thank you for your advice.
</div>