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


Golang Context.HttpRequest方法代码示例

本文整理汇总了Golang中github.com/stretchrcom/goweb/context.Context.HttpRequest方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.HttpRequest方法的具体用法?Golang Context.HttpRequest怎么用?Golang Context.HttpRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/stretchrcom/goweb/context.Context的用法示例。


在下文中一共展示了Context.HttpRequest方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: WriteResponseObject

// WriteResponseObject writes the status code and response object to the HttpResponseWriter in
// the specified context, in the format best suited based on the request.
//
// Goweb uses the WebCodecService to decide which codec to use when responding
// see http://godoc.org/github.com/stretchrcom/codecs/services#WebCodecService for more information.
//
// This method should be used when the Goweb Standard Response Object does not satisfy the needs of
// the API, but other Respond* methods are recommended.
func (a *GowebAPIResponder) WriteResponseObject(ctx context.Context, status int, responseObject interface{}) error {

	service := a.GetCodecService()

	acceptHeader := ctx.HttpRequest().Header.Get("Accept")
	extension := ctx.FileExtension()
	hasCallback := len(ctx.QueryValue("callback")) > 0

	codec, codecError := service.GetCodecForResponding(acceptHeader, extension, hasCallback)

	if codecError != nil {
		return codecError
	}

	var options map[string]interface{}

	// do we need to add some options?
	if hasCallback {
		options = map[string]interface{}{constants.OptionKeyClientCallback: ctx.QueryValue("callback")}
	}

	output, marshalErr := codec.Marshal(responseObject, options)

	if marshalErr != nil {
		return marshalErr
	}

	// use the HTTP responder to respond
	ctx.HttpResponseWriter().Header().Set("Content-Type", codec.ContentType()) // TODO: test me
	a.httpResponder.With(ctx, status, output)

	return nil

}
开发者ID:smw,项目名称:goweb,代码行数:42,代码来源:goweb_api_responder.go

示例2: Read

//add error catching here.
func (r *UniversumController) Read(brand string, ctx context.Context) error {

	fmt.Println("rest")
	query := ctx.HttpRequest().URL.RawQuery
	//metric_name := GetQueryValue("name",query)
	since_str := GetQueryValue("since", query)
	const shortForm = "2006-Jan-02"
	since, _ := time.Parse(shortForm, since_str)

	until_str := GetQueryValue("until", query)
	until, _ := time.Parse(shortForm, until_str)
	resolution := "day"

	key := GetQueryValue("apikey", query)
	fmt.Println("the api key is " + key)
	user_id := db.Read("universum", "apikeys", key)

	if len(user_id) < 1 {
		errObject := "unverified api key"
		return goweb.API.RespondWithError(ctx, 500, errObject)
	}

	if brand == "ranking" {
		ranking := new(RankingData)
		ranking.Type = brand
		ranking.Brands, ranking.Change = GetMetrics(brand, query, since, until, resolution)
		return goweb.API.RespondWithData(ctx, ranking)
	}

	data := new(Data)
	data.Brand = brand
	data.Metrics, data.TimeStamp = GetMetrics(brand, query, since, until, resolution)

	fmt.Println("booyay")
	return goweb.API.RespondWithData(ctx, data)

}
开发者ID:sridif,项目名称:greengridz,代码行数:38,代码来源:controller.go


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