6

Anyone know of a way to preserve the order of response headers received using the net/http package?

As it is, the headers are accessible only as a map[string]string and iterating over them does not preserve order.

Any input would be much appreciated, thanks all!

brun
  • 133
  • 9
  • 9
    Not with the stdlib http client, since as you said, they are stored in a map. Though, since the spec states that the order of different http headers does not matter, what problem are you trying to solve? There is not even a guarantee that the order you receive the headers in is the same as what the server sent out. – JimB Mar 23 '18 at 21:29
  • Adding to @JimB, any proxy along the path may also change things. So trying to go after some kind of order is clealy the wrong answer to whatever problem. I believe than in HTTP/2 things would be even more complicated. – Patrick Mevzek Mar 23 '18 at 21:41
  • By the way, `map` in go doesn't preserve order by it's nature. – Oleg Kovalov Mar 24 '18 at 06:32
  • I understand map does not preserve order but that doesn't mean someone has worked up a reflect type way to access the original raw headers read in. Furthermore, I stated in my completely unedited original question that a map does not preserve order. My profile says I have a -2 from this question, if you're going to downvote please post a reason so the community can learn, thank you. – brun May 16 '18 at 19:39
  • Also JimB and @Patrick Mevzek I understand that the header order may not matter to everyone but JimB brings up one of the points i'm looking for. If you know how a server or client normally acts via their headers, then seeing a change like this will show that they may have moved behind a proxy. I do not understand how the "why" matters as much as the "how". If you want to understand why someone would want to preserve order of received headers, then maybe that should be opened as it's own question on stack overflow. – brun May 16 '18 at 19:44
  • @brun I am sorry to disagree. Since nothing in the spec says some order must exist, a server could change at any time for any reason and this fact alone does not give you any factual data about it being moved besides a proxy. The opposite holds too: it could be moved besides a proxy, while nothing changes in the headers, so you would not have any clue that happened by doing searches on the order. Sorry, I still believe you are trying to solve the wrong problem here. Anyway find a library "lower" than `net/http` or just hack that one to go after your ordering wild chase... – Patrick Mevzek May 16 '18 at 19:49
  • 1
    @PatrickMevzek You can disagree, but it's not a debate or an argument. I asked a question to see if anyone has an answer for. I want to do research, and that is the reason I desire the order of the headers. I don't understand why that has become the focus? – brun May 17 '18 at 14:17
  • 1
    For anyone looking at this, an answer is provided here: https://stackoverflow.com/questions/54086270/get-raw-header-of-http-response `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.` You can access raw headers with a custom wrapper and parse them in original order. – KawaLo Feb 09 '22 at 13:55

0 Answers0