当前位置: 首页>>代码示例>>Golang>>正文


Golang Response.WriteJsonWithStatus方法代码示例

本文整理汇总了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),
	})
}
开发者ID:speedland,项目名称:apps,代码行数:9,代码来源:api.go

示例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,
	})
}
开发者ID:speedland,项目名称:apps,代码行数:14,代码来源:api.go

示例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)
}
开发者ID:speedland,项目名称:rakugaki,代码行数:20,代码来源:posts.go

示例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)
}
开发者ID:speedland,项目名称:rakugaki,代码行数:41,代码来源:posts.go

示例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)
}
开发者ID:speedland,项目名称:rakugaki,代码行数:17,代码来源:blogs.go

示例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)
}
开发者ID:speedland,项目名称:rakugaki,代码行数:22,代码来源:blogs.go

示例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.",
	})
}
开发者ID:speedland,项目名称:apps,代码行数:7,代码来源:api.go

示例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",
	})
}
开发者ID:speedland,项目名称:apps,代码行数:6,代码来源:api.go

示例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),
	})
}
开发者ID:speedland,项目名称:apps,代码行数:7,代码来源:api.go


注:本文中的github.com/speedland/wcg.Response.WriteJsonWithStatus方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。