preserve order of http response headers golang
Asked Answered
P

0

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!

Prune answered 23/3, 2018 at 21:18 Comment(8)
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.Cryptology
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.Mier
By the way, map in go doesn't preserve order by it's nature.Gen
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.Prune
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.Prune
@Prune 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...Mier
@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?Prune
For anyone looking at this, an answer is provided here: #54086770 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.Wreath

© 2022 - 2024 — McMap. All rights reserved.