duanjie2940 2016-06-21 05:30
浏览 41
已采纳

转到Web服务-将tar.gz文件作为请求正文

I need to implement web service in go that processes tar.gz files and I wonder what is the correct way, what content type I need to define, etc.

plus, I found that a lot of things are handled automatically - on the client side I just post a gzip reader as request body and Accept-Encoding: gzip header is added automatically, and on the server side - I do not need to gunzip the request body, it is already extracted to tar. does that make sense?

Can I rely that it would be like this with any client?

Server:

func main() {

    router := mux.NewRouter().StrictSlash(true)
    router.Handle("/results", dataupload.NewUploadHandler()).Methods("POST")
    log.Fatal(http.ListenAndServe(*address, router))
}

Uploader:

package dataupload

import (
    "errors"
    log "github.com/Sirupsen/logrus"
    "io"
    "net/http"
)

// UploadHandler responds to /results http request, which is the result-service rest API for uploading results
type UploadHandler struct {
    uploader Uploader
}

// NewUploadHandler creates UploadHandler instance
func NewUploadHandler() *UploadHandler {

    return &UploadHandler{
        uploader: TarUploader{},
    }
}

func (uh UploadHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {

    retStatus := http.StatusOK
    body, err := getBody(request)
    if err != nil {
        retStatus = http.StatusBadRequest
        log.Error("Error fetching request body. ", err)
    } else {
        _, err := uh.uploader.Upload(body)
    }
    writer.WriteHeader(retStatus)
}

func getBody(request *http.Request) (io.ReadCloser, error) {

    requestBody := request.Body
    if requestBody == nil {
        return nil, errors.New("Empty request body")
    }
    var err error
    // this part is commented out since somehow the body is already gunzipped - no need to extract it.
    /*if strings.Contains(request.Header.Get("Accept-Encoding"), "gzip") {
        requestBody, err = gzip.NewReader(requestBody)
    }*/

    return requestBody, err
}

Client

func main() {

    f, err := os.Open("test.tar.gz")
    if err != nil {
        log.Fatalf("error openning file %s", err)
    }
    defer f.Close()
    client := new(http.Client)
    reader, err := gzip.NewReader(f)
    if err != nil {
        log.Fatalf("error gzip file %s", err)
    }
    request, err := http.NewRequest("POST", "http://localhost:8080/results", reader)
    _, err = client.Do(request)
    if err != nil {
        log.Fatalf("error uploading file %s", err)
    }
}
  • 写回答

2条回答 默认 最新

  • dpq755012465 2016-06-21 07:32
    关注

    The code you've written for the client is just sending the tarfile directly because of this code:

    reader, err := gzip.NewReader(f)
    ...
    request, err := http.NewRequest("POST", "http://localhost:8080/results", reader)
    

    If you sent the .tar.gz file content directly, then you would need to gunzip it on the server. E.g.:

    request, err := http.NewRequest(..., f)
    

    I think that's closer to the behavior you should expect third-party clients to exhibit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码