douyan7916 2016-10-06 09:16
浏览 53
已采纳

如何在GO中等待POST请求完成?

Hi I am creating a POST request in GO but it terminates before actually finishing, for example I am trying to pull a docker image and this can be done with curl by the following:

curl -X POST https://address/images/create?fromImage=imagename 

this returns the following

{"status":"Pulling from imagename","id":"latest"}
{"status":"Already exists","progressDetail":{},"id":"3d30e94188f7"}
{"status":"Already exists","progressDetail":{},"id":"bf4e27765153"}
{"status":"Already exists","progressDetail":{},"id":"67280fd39fba"}
.... many of those
{"status":"Pull complete","progressDetail":{},"id":"21c062e2346f"}
{"status":"Digest: sha256:24f26a1344fca6d5ee1adcdsf2d01b20d7823c560ed9d2193466e36bd1f049088"}
{"status":"Pulling from imagename","id":"20161005"}
{"status":"Digest: sha256:f527dsfds88676eb25d8f7de5406f46cbc3a995345ddb4bb3d08fcf110458fe3cf"}
{"status":"Status: Downloaded newer image for imagename"}

and the image is pulled successfully

but If I try from go

func PullImage(imagename string, uuid string) error {
logFields := log.Fields{

    "handler": "PullImage",
    "uuid":    uuid,
}

log.WithFields(logFields).Debugf("imagename:%v", imagename)

url := fmt.Sprintf("https://%s/images/create?fromImage%s", sconf.Docker.Endpoint, imagename)
req, err := http.NewRequest("POST", url, nil)

req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)

log.WithFields(logFields).Infof("call url:%s", url)
if err != nil {
    log.WithFields(logFields).Errorf("Error in call url (%s) :%s", url, err)

    return errors.New(fmt.Sprintf("Error in call url (%s) :%s", url, err))
}


var pullresbody interface{}
err = json.NewDecoder(resp.Body).Decode(&pullresbody)
if err != nil {
    log.WithFields(logFields).Errorf("Could not unmarshal json: %s", err)
    return errors.New(fmt.Sprintf("Could not unmarshal json: %s", err))
}

    log.WithFields(logFields).Infof("response of url %s:%+v", url, resp)
log.WithFields(logFields).Infof("response body:%+v", pullresbody)
   return nil

}

then I get this in the logs:

map[status:Pulling from imagename]

and the image is not pulled so the connection is stopped before really finishing how can I fix this?

  • 写回答

1条回答 默认 最新

  • dsc862009 2016-10-06 10:03
    关注

    The Decoder will only decode one object from a stream at a time. To get all objects, you'll need something like

    var pullRespBody interface{}
    dec := json.NewDecoder(resp.Body)
    var err error
    for err == nil {
        err = dec.Decode(&pullRespBody)
        // Check err...
        log.WithFields(logFields).Infof("response body:%+v", pullRespBody)
        // Do something else with pullRespBody...
    }
    // Deal with err...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据