在Datastore中编写请求的主体部分。

huangapple go评论124阅读模式
英文:

Write the body of a request in datastore

问题

如何在Datastore中编写请求的主体?

在我的func init()函数中,我使用gorilla mux声明了我的路由器,所以如果我向/add发送POST请求,我将需要向Datastore添加一些数据,但是我刚刚开始使用Datastore,所以我不太清楚该怎么做。

我声明了一个名为Item的结构体:

  1. type Item struct {
  2. ID int64
  3. Type string `json:"type"`
  4. }

路由器将重定向到函数CItem

  1. func CItem(w http.ResponseWriter, r *http.Request) {
  2. var item Item
  3. data := json.NewDecoder(r.Body).Decode(&item)
  4. defer r.Body.Close()
  5. fmt.Fprintln(w, data)
  6. }

但是,当我使用paw发送POST请求时,例如:

  1. invalid character 'y' in literal true (expecting 'r')

或者使用curl:

  1. curl -X POST -d "{\"type\": \"that\"}" http://localhost:8080/add

我该如何解决这个问题?接下来我需要做什么才能将我的数据存储在Datastore中?一个简单的示例将很好。

英文:

How can I write the body of a request in datastore?

In my func init() I declare my router using gorilla mux, so that if I do a post request to /add I will need to add some data to datastore, but I am just starting with datastore so I don't really know how to.

I have declared a struct item

  1. type Item Struct {
  2. ID int64
  3. Type string `json:type`
  4. }

The router will redirect to the function CItem

  1. func CItem(w http.ResponseWriter, r *http.Request) {
  2. var item Item
  3. data := json.NewDecoder(r.Body).Decode(&item)
  4. defer r.Body.Close()
  5. fmt.Fprintln(w, data)
  6. }

But when I do a post request using paw for example I get:
invalid character 'y' in literal true (expecting 'r')

Or using curl:
curl -X POST -d "{\"type\": \"that\"}" http://localhost:8080/add

How can I fix this, and what do I need to do next to store my data in datastore a small example will be nice.

答案1

得分: 2

以下是对你的代码的一些评论以及一个快速示例,展示如何存储实体:

  1. type Item struct {
  2. ID int64
  3. Type string `json:"type"` // <-- 需要引号
  4. }
  5. func CItem(w http.ResponseWriter, r *http.Request) {
  6. var item Item
  7. err := json.NewDecoder(r.Body).Decode(&item) // <-- 解码返回错误,而不是数据
  8. if err != nil {
  9. http.Error(w, err.Error(), 400)
  10. return
  11. }
  12. // defer r.Body.Close() <-- 不需要关闭请求体
  13. fmt.Fprintln(w, item) // <-- 打印解码后的项
  14. c := appengine.NewContext(r)
  15. key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "citem", nil), &item)
  16. if err != nil {
  17. http.Error(w, err.Error(), http.StatusInternalServerError)
  18. return
  19. }
  20. fmt.Fprintln(w, "key is", key)
  21. }

希望这可以帮助到你!

英文:

Here are some comments on your code so far and a quick example showing how to store the entity:

  1. type Item Struct {
  2. ID int64
  3. Type string `json:&quot;type&quot;` // &lt;-- quotes needed
  4. }
  5. func CItem(w http.ResponseWriter, r *http.Request) {
  6. var item Item
  7. err := json.NewDecoder(r.Body).Decode(&amp;item) // &lt;-- decode returns an error, not data
  8. if err != nil {
  9. http.Error(w, err.Error(), 400)
  10. return
  11. }
  12. // defer r.Body.Close() &lt;-- no need to close request body
  13. fmt.Fprintln(w, item) // &lt;-- print the decoded item
  14. c := appengine.NewContext(r)
  15. key, err := datastore.Put(c, datastore.NewIncompleteKey(c, &quot;citem&quot;, nil), &amp;item)
  16. if err != nil {
  17. http.Error(w, err.Error(), http.StatusInternalServerError)
  18. return
  19. }
  20. fmt.Fprintln(w, &quot;key is&quot;, key)
  21. }

答案2

得分: 1

所以你将有一个描述请求的类,另一个描述NDB/DB实体的类。你需要手动将请求中的数据点映射到数据存储对象,然后保存它。

英文:

So you'll have a class describing the request and another describing the NDB/DB entity. You'll have to manually map the data points from the request to the datastore object and then save it

huangapple
  • 本文由 发表于 2014年10月22日 03:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/26494804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定