Search code examples
httpgoresponse

Get RAW header of http response


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?


Solution

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