doulei3488 2015-06-07 14:19
浏览 25

从任务队列解组机体响应

I'm using go and google task queue in order to create some a sync jobs.

I'm passing the data to the worker method successfully but i can't unmarshal the data in order to use it.

I tried different ways i'm getting an unmarshal error

 err um &json.SyntaxError{msg:"invalid character 'i' in literal false (expecting 'a')", Offset:2}

This is how i'm sending the data to the queue

    keys := make(map[string][]string)
    keys["filenames"] = req.FileNames // []string

    t := taskqueue.NewPOSTTask("/deletetask", keys)
    _, err = taskqueue.Add(ctx, t, "delete")

And this is how i tried to unmarshal it

 type Files struct {
fileNames string `json:"filenames"`
 }


func worker(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)

var objmap map[string]json.RawMessage
b, err := ioutil.ReadAll(r.Body)
if err != nil {
    c.Debugf("err io  %#v", err)
}
c.Debugf("b  %#v", string(b[:])) //Print: b  "filenames=1.txt&filenames=2.txt"

err = json.Unmarshal(b, &objmap)
if err != nil {
    c.Debugf("err um %#v", err)
}
 //this didn't work as well  same err
    f := []Files{}
    err = json.Unmarshal(b, &f)
 }
  • 写回答

1条回答 默认 最新

  • duannao3819 2015-06-08 14:16
    关注

    Arguments to tasks are sent as POST-values, you assign a slice of strings as the POST-value for the key filenames.

    Then you try to deserialize the full POST request body as JSON.

    A simple solution would be to split up each file in one task and just send the file name as a string value, then it would be something like:

    // Add tasks
    for i := range req.FileNames {
        postValues := url.Values{}
        postValues.Set("fileName", req.FileNames[i])
    
        t := taskqueue.NewPOSTTask("/deletetask", postValues)
        if _, err := taskqueue.Add(ctx, t, "delete"); err != nil {
            c.Errorf("Failed add task, error: %v, fileName: %v", err, req.FileNames[i])
        }
    }
    
    // The actual delete worker
    func worker(w http.ResponseWriter, r *http.Request) {
        ctx := appengine.NewContext(r)
        fileName := r.FormValue("fileName")
    
        ctx.Infof("Deleting: %v", fileName)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分