本文整理汇总了Golang中github.com/speedland/wcg.Response.WriteJsonWithStatus方法的典型用法代码示例。如果您正苦于以下问题:Golang Response.WriteJsonWithStatus方法的具体用法?Golang Response.WriteJsonWithStatus怎么用?Golang Response.WriteJsonWithStatus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/speedland/wcg.Response
的用法示例。
在下文中一共展示了Response.WriteJsonWithStatus方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Created
// HTTP 201 Created
func (api *ApiHelper) Created(res *wcg.Response, req *wcg.Request, id string) {
loc := path.Join(req.URL().Path, fmt.Sprintf("%s.json", id))
res.WriteJsonWithStatus(201, nil, map[string]interface{}{
"ok": true,
"id": id,
"location": wcg.AbsoluteUrl(req, loc),
})
}
示例2: InternalError
// HTTP 500 Internal Error
func (api *ApiHelper) InternalError(res *wcg.Response, req *wcg.Request, e error) {
var msg string
if util.IsProduction() {
msg = "Sorry for inconvinience. An internal error occurred. Please try again later."
} else {
msg = fmt.Sprintf("An error occurred: %v", e)
}
req.Logger.Error("Serve 500: %v", e)
res.WriteJsonWithStatus(500, nil, map[string]interface{}{
"error": "internal_server_error",
"message": msg,
})
}
示例3: deletePostHandler
func deletePostHandler(res *wcg.Response, req *wcg.Request) {
blog, _, err := fetchBlogForUpdate(res, req, false)
if err != nil {
return
}
post, err := fetchPostForUpdate(req, res, blog)
if err != nil {
return
}
err = models.NewPostDriver(gae.NewContext(req), req.Logger).DeletePost(post)
if err != nil {
if supports.IsValidationError(err) {
res.WriteJsonWithStatus(400, nil, err)
} else {
res.RenderInternalError(err.Error())
}
return
}
res.WriteJsonWithStatus(200, nil, ok)
}
示例4: updatePostHandler
func updatePostHandler(res *wcg.Response, req *wcg.Request) {
blog, _, err := fetchBlogForUpdate(res, req, false)
if err != nil {
return
}
post, err := fetchPostForUpdate(req, res, blog)
if err != nil {
return
}
post.Title = req.Form("title")
post.Content = req.Form("content")
post.IsDraft = req.Form("is_draft") == "true"
post.IsNew = false
post.PostDate, err = time.Parse(wcg.FormDateFormat, req.Form("post_date"))
post.Tags = strings.Split(req.Form("tags"), ",")
for i, v := range post.Tags {
post.Tags[i] = strings.TrimSpace(v)
}
if err != nil {
res.WriteHeader(400)
res.WriteString("Invalid date format.")
res.End()
}
driver := models.NewPostDriver(gae.NewContext(req), req.Logger)
if AppConfig.GithubMarkdown {
driver.HttpClient = gae.NewHttpClient(req)
}
err = driver.SavePost(post)
if err != nil {
if supports.IsValidationError(err) {
res.WriteJsonWithStatus(400, nil, err)
} else {
res.RenderInternalError(err.Error())
}
return
}
req.Logger.Info("A post is created at %s.", post.Id)
res.WriteJsonWithStatus(200, nil, ok)
}
示例5: deleteBlogHandler
func deleteBlogHandler(res *wcg.Response, req *wcg.Request) {
driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
err := driver.DeleteBlog(req.Param("blog_id"), req.User.Id())
if err != nil {
if supports.IsValidationError(err) {
res.WriteJsonWithStatus(400, nil, err)
} else if err == models.ErrBlogNotOwned {
res.WriteJsonWithStatus(403, nil, no_permission)
} else if err == models.ErrBlogNotFound {
res.WriteJsonWithStatus(404, nil, not_found)
} else {
res.RenderInternalError(err.Error())
}
return
}
res.WriteJsonWithStatus(200, nil, ok)
}
示例6: createBlogHandler
func createBlogHandler(res *wcg.Response, req *wcg.Request) {
driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
blog, err := driver.CreateBlog(
req.Form("path"),
req.Form("title"),
req.Form("description"),
req.User.Id(),
)
if err != nil {
if err == models.ErrBlogAlreadyExists {
res.WriteJsonWithStatus(409, nil, already_taken)
} else if supports.IsValidationError(err) {
res.WriteJsonWithStatus(400, nil, err)
} else {
res.RenderInternalError(err.Error())
}
return
}
res.WriteJsonWithStatus(201, map[string]string{
"location": wcg.AbsoluteUrl(req, "/"+blog.Id),
}, ok)
}
示例7: NotFound
// HTTP 400 Bad Request
func (api *ApiHelper) NotFound(res *wcg.Response, req *wcg.Request) {
res.WriteJsonWithStatus(404, nil, map[string]interface{}{
"error": "not_found",
"message": "The requested resource is not found on this server.",
})
}
示例8: Forbidden
func (api *ApiHelper) Forbidden(res *wcg.Response, req *wcg.Request) {
res.WriteJsonWithStatus(403, nil, map[string]interface{}{
"erorr": "forbidden",
"message": "You don't have enough permission to access the resource",
})
}
示例9: BadRequest
// HTTP 400 Bad Request
func (api *ApiHelper) BadRequest(res *wcg.Response, req *wcg.Request, e error) {
res.WriteJsonWithStatus(400, nil, map[string]interface{}{
"error": "bad_request",
"message": fmt.Sprintf("%v", e),
})
}