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

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

How to wait till POST request is finished in GO?

问题

你好,我正在使用GO创建一个POST请求,但是它在实际完成之前就终止了。例如,我正在尝试使用curl拉取一个Docker镜像,可以使用以下命令完成:

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

这将返回以下内容:

{"status":"Pulling from imagename","id":"latest"}
{"status":"Already exists","progressDetail":{},"id":"3d30e94188f7"}
{"status":"Already exists","progressDetail":{},"id":"bf4e27765153"}
{"status":"Already exists","progressDetail":{},"id":"67280fd39fba"}
.... 许多类似的输出
{"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"}

并且成功拉取了镜像。

但是,如果我尝试使用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
}

然后在日志中我得到了这个:

map[status:Pulling from imagename]

并且镜像没有被拉取,所以连接在真正完成之前就被停止了。我该如何解决这个问题?

英文:

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

得分: 3

Decoder每次只能从流中解码一个对象。要获取所有的对象,你需要像下面这样做:

var pullRespBody interface{}
dec := json.NewDecoder(resp.Body)
var err error
for err == nil {
    err = dec.Decode(&pullRespBody)
    // 检查 err...
    log.WithFields(logFields).Infof("response body:%+v", pullRespBody)
    // 对 pullRespBody 进行其他操作...
}
// 处理 err...

这段代码使用了json包中的Decoder来解码JSON对象。通过循环调用Decode方法,可以逐个获取所有的对象。在每次解码后,你可以对pullRespBody进行其他操作。最后,你可以处理可能出现的错误(err)。

英文:

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...

huangapple
  • 本文由 发表于 2016年10月6日 17:16:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/39892119.html
匿名

发表评论

匿名网友

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

确定