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

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

Write the body of a request in datastore

问题

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

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

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

type Item struct {
  ID   int64
  Type string `json:"type"`
}

路由器将重定向到函数CItem

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

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

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

或者使用curl:

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

type Item Struct {
  ID int64
  Type string `json:type`
}

The router will redirect to the function CItem

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

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

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

type Item struct {
  ID   int64
  Type string `json:"type"` // <-- 需要引号
}

func CItem(w http.ResponseWriter, r *http.Request) {
  var item Item
  err := json.NewDecoder(r.Body).Decode(&item) // <-- 解码返回错误,而不是数据
  if err != nil {
    http.Error(w, err.Error(), 400)
    return
  }
  // defer r.Body.Close()  <-- 不需要关闭请求体
  fmt.Fprintln(w, item) // <-- 打印解码后的项

  c := appengine.NewContext(r)
  key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "citem", nil), &item)
  if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }
  fmt.Fprintln(w, "key is", key)
}

希望这可以帮助到你!

英文:

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

type Item Struct {
  ID int64
  Type string `json:&quot;type&quot;` // &lt;-- quotes needed
}

func CItem(w http.ResponseWriter, r *http.Request) { 
   var item Item
   err := json.NewDecoder(r.Body).Decode(&amp;item) // &lt;-- decode returns an error, not data
   if err != nil {
        http.Error(w, err.Error(), 400)
        return
   }
   // defer r.Body.Close()  &lt;-- no need to close request body
   fmt.Fprintln(w, item) // &lt;-- print the decoded item

   c := appengine.NewContext(r)
   key, err := datastore.Put(c, datastore.NewIncompleteKey(c, &quot;citem&quot;, nil), &amp;item)
   if err != nil {
       http.Error(w, err.Error(), http.StatusInternalServerError)
       return
   }
   fmt.Fprintln(w, &quot;key is&quot;, key)
}

答案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:

确定