FAQ
Howdy,

I would like to both parse the Body of an http.Request as JSON and also
hash the Body's entire byte contents. What is the best way to do this
efficiently?

Thank you,

Alvaro

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Rémy Oudompheng at Jun 26, 2013 at 10:24 pm

    2013/6/27 Alvaro Genial <genial@alva.ro>:
    Howdy,

    I would like to both parse the Body of an http.Request as JSON and also hash
    the Body's entire byte contents. What is the best way to do this
    efficiently?
    Use a io.TeeReader so that everything you read goes to the hasher.

    Rémy.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nick Craig-Wood at Jun 26, 2013 at 10:30 pm

    On 26/06/13 23:21, Alvaro Genial wrote:
    I would like to both parse the Body of an http.Request as JSON and also
    hash the Body's entire byte contents. What is the best way to do this
    efficiently?
    Use an io.TeeReader: http://golang.org/pkg/io/#TeeReader

    Something like this maybe (adapted from
    https://github.com/ncw/swift/blob/master/swift.go)

      hash := md5.New()
      var body io.Reader = response.Body
      if checkHash {
       body = io.TeeReader(contents, hash)
      }
             // read stuff from body as normal
      if checkHash {
       receivedMd5 := strings.ToLower(headers["Etag"])
       calculatedMd5 := fmt.Sprintf("%x", hash.Sum(nil))
       if receivedMd5 != calculatedMd5 {
        // ObjectCorrupted
       }
      }


    --
    Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 26, '13 at 10:21p
activeJun 26, '13 at 10:30p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase