Reputation: 1428
How to get raw header of response as string like this:
alt-svc: quic=":443"; ma=2592000; v="44,43,39,35"
cache-control: private, max-age=0
content-encoding: br
content-type: text/html; charset=UTF-8
date: Tue, 08 Jan 2019 06:19:47 GMT
expires: -1
server: gws
set-cookie: 1P_JAR=2019-01-08-06; expires=Thu, 07-Feb-2019 06:19:47 GMT; path=/; domain=.google.com
set-cookie: SIDCC=ABtHo-HHNcja-cEEFEUXtBmLOdql4RTVMCWKGApEFFb8lWSAqaTF_fi0gDLoWaCzH3ogvEofah0; expires=Mon, 08-Apr-2019 06:19:47 GMT; path=/; domain=.google.com; priority=high
status: 200
Because I want get the multiple set-cookie
value from the response header.
Use Http.Response.Header.Get("set-cookies")
just return the last row.
Or how can I get the multiple cookies?
Upvotes: 1
Views: 5401
Reputation: 1
In my opinion, the best option for roundtripping a response is
httputil#DumpResponse
:
package raw
import (
"bufio"
"bytes"
"net/http"
"net/http/httputil"
)
func encode(res *http.Response) ([]byte, error) {
return httputil.DumpResponse(res, false)
}
func decode(data []byte) (*http.Response, error) {
return http.ReadResponse(bufio.NewReader(bytes.NewReader(data)), nil)
}
Or, if you just want the cookies, you can do this:
package raw
import (
"encoding/json"
"net/http"
)
func encode(res *http.Response) ([]byte, error) {
return json.Marshal(res.Cookies())
}
func decode(data []byte) ([]http.Cookie, error) {
var c []http.Cookie
if e := json.Unmarshal(data, &c); e != nil {
return nil, e
}
return c, nil
}
Or for a single cookie:
package raw
import (
"encoding/json"
"net/http"
)
func encode(res *http.Response, name string) ([]byte, error) {
for _, c := range res.Cookies() {
if c.Name == name {
return json.Marshal(c)
}
}
return nil, http.ErrNoCookie
}
func decode(data []byte) (*http.Cookie, error) {
c := new(http.Cookie)
if e := json.Unmarshal(data, c); e != nil {
return nil, e
}
return c, nil
}
https://golang.org/pkg/net/http/httputil#DumpResponse
Upvotes: 2
Reputation: 79764
If you want the raw headers, you'll need to write some wrapper for net.Conn
which captures the raw header before it is interpreted by the http
library.
But you don't really seem to need the raw header--or even the full header at all. If your goal is simply to read multiple cookies, the easiest way to do this is with the Cookies
method on the response.
An intermediate option between these two is to read the entire Header
field of the response. This will present the full header, but its order is not guaranteed, and minimal parsing will have been done (to remove newlines, etc), so it can't be said this is truly "raw". It does, however, preserve multiple values, in case of duplicate headers, by storing all header values in a []string
. So for the purposes of this question, this should be perfectly adequate (although Response.Cookies
, as mentioned above, would be easier).
Upvotes: 5
Reputation: 5175
The standard http lib parsed the header by default.
Using fasthttp (you will need to re-write your router and handler function) will give you ability to get the raw header.
Upvotes: 1
Reputation: 1428
use fmt.Printf("%+v", resp.Header)
can write all header to a string like below:
map[Set-Cookie:[CASPRIVACY=""; Path=/ CASTGC=TGT-1248283-0BRja0uNQR9PntBl4HnrzKA44q5XDQXxA5FRVcnO9Xn6CkTU6S-passport; Path=/ CASLOGC=%7B%22myuniRole%22%3A0%2C%22username%22%3A%22a6051b9d08384f3ea2d203e744d3f7e6%22%2C%22mycuRole%22%3A0%2C%22userId%22%3A192676671%2C%22myinstRole%22%3A0%2C%22realName%22%3A%22%E9%9B%B7%E7%BA%A2%E6%A2%85%22%2C%22uuid%22%3A%22V4eoYpNB%22%2C%22headPic%22%3A%22http%3A%2F%2Fimage.xxx.com%2Fzhs%2Fapp%2Fcontent%2F201812%2Ff0b20d740df04d3196a20bc39f58ab56_s3.png%22%7D; Domain=.xxxx.com; Path=/ SERVERID=958b97eacbe3c49360ace2dfc0bd31b4|1546915535|1546915533;Path=/] Pragma:[no-cache] Expires:[Thu, 01 Jan 1970 00:00:00 GMT] Cache-Control:[no-cache no-store] Location:[http://online.xxx.com/onlineSchool/?ticket=ST-3285248-UiTbNt7bnUwXdtWSznIT-passport.xxxx.com] Date:[Tue, 08 Jan 2019 02:45:35 GMT] Content-Length:[0] Connection:[keep-alive]]
Is hardly to get all cookies from the string by use split and regx
Upvotes: -3