New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide example for SetExtraHTTPHeaders #54
Comments
I wasn't sure at first why you needed an example of this, but I took a look at the input arguments to the function and noticed what might be the reason. Headers are defined as: // Headers Request / response headers as keys / values of JSON object.
type Headers []byte It is kept as a byte slice (raw JSON) to make it more convenient to decode headers into any type of struct (instead of, say a Maybe I could add a convenience method to create headers from a map as well, something like: func HeadersFromMap(m map[string]string) (Headers, error) {
b, err := json.Marshal(m)
if err != nil {
return nil, err
}
return b, nil
} Thoughts? |
@mafredri would be nice, more readable that way. only map needs to defined from outside of library |
How do you mean? |
var headers map[string]string |
I'm not sure I follow, e.g. what's the problem? You could just as well do: HeadersFromMap(map[string]string{"Accept-Language": "*"}) I was thinking of a potential parallel method (or alternative, haven't decided quite on what API additions to introduce) const missingValue = "(MISSING)"
func (n Headers) FromKV(keyvals ...string) Headers {
if len(keyvals) == 0 {
return nil
}
if len(keyvals)%2 != 0 {
keyvals = append(keyvals, missingValue)
}
m := make(map[string]string)
for i := 0; i < len(keyvals); i = i + 2 {
m[keyvals[i]] = keyvals[i+1]
}
b, err := json.Marshal(m)
if err != nil {
return []byte(err.Error())
}
return b
} I'm not sure if this is an acceptable way to avoid returning an error. Returning an error would make this much less fluid, and I don't think encoding a map[string]string to JSON should ever return an error, so it would be unfortunate to return one. If there ever was an error, the headers would become invalid JSON (via |
You can return empty []byte on error |
But this would not inform the user that encoding failed, and debugging why something isn't working (or headers missing) would be non-obvious. |
User expect that returned value is non empty, so if it is actually empty, this is error although detailed err message would be hidden |
Why would you say that is? Let's say In my opinion, a function that returns no error can also never fail (except maybe panic). Or at least, that is the common expectation for users. |
If it is a helper function, user will use it for marshalling supplied headers, so nil output would be an error from users point of view. In this case user should validate input, that it is not empty ...slice or map, if it is not and func returns zero-sized variable this will tell that error accrued. I agree on usage case of panic, but panic often complicates things. Can we just return data and error? |
I don't trust that every user will see this the same way. Consider the following (given above implementation): var keyvals []string
if shouldAddHeadersY {
keyvals = append(keyvals, "My-AwesomeY", "Header")
}
if shouldAddHeadersX {
keyvals = append(keyvals, "My-AwesomeX", "Header")
}
headers := network.HeadersFromKV(keyvals...)
c.Network.SetExtraHTTPHeaders(headers) I'm struggling to see any use case where
Absolutely, and this is why
That's probably best, yeah :) |
Any time frame on this? I am eager to test this out :) |
Sorry, can't provide any guarantees. I'll try to get around to it soon, probably during next week at the latest. |
hi @mafredri, any news on that? |
Sorry, still haven’t gotten around to give this enough thought. I try to avoid making breaking changes which unfortunately also means I end up spending a lot of time thinking of solutions before pushing them out 😕. |
I'm struggling with finding a good purpose for adding this to the library. Considering the The only reason to add it, that I can think of, is to:
But IMO this could be accomplished by improving the code comment for What are your thoughts @s3rj1k? |
Hi @mafredri, you can just provide detailed example on how to do it and no changes will be required for library. |
I'm not sure I understand what type of example you're asking for? You could use it like this: err := c.Network.SetExtraHTTPHeaders(ctx,
network.NewSetExtraHTTPHeadersArgs(
[]byte(`{"Content-Type": "text/html; charset=utf-8"}`),
),
) But IMO this information is already available in this thread, is it something else you're looking for? |
@mafredri Hi, sorry for late reply can't get working |
Can you post the code that you've tried to use? I'll try to have a look during this week. |
https://gist.github.com/s3rj1k/fa6d9b4da2a97666a6eb20d3e3fd077c modified test case, output should include [HTTP_ACCEPT_LANGUAGE] header |
Have you tried enabling the Network domain? After adding |
I'm having this same problem. I'm using Chrome Version 68.0.3440.106. I used your suggestion to eliminate any other possibilities. My code looks exactly like the following. I tested it with google.com and it creates the pdf without any issues but it doesn't work on a site that actually needs the header.
|
@vjiandani-r7 I really do recommend checking for errors, for example, the JSON you are using in Hope that fixes it! |
I added in error handling and confirmed that I fixed the error It still doesn't work. |
@vjiandani-r7 how did you confirm that it doesn't work? Have you looked at the headers sent to the webserver? Here's a gist of what I've used to test this and there are no issues setting / sending the headers. |
@mafredri Hmm I confirmed with your gist that the headers are being passed along correctly but I'm still having issues. I'll have to do some more debugging to figure out why the page I'm trying to load thinks they're missing. Thanks for your help. |
@vjiandani-r7 alright, I hope you figure it out! @s3rj1k I hope this issue was resolved for you, feel free to re-open if there's anything left to discuss. |
Note that network enable seems to be necessary for this to work: mafredri/cdp#54 (comment)
I was looking for example SetExtraHTTPHeaders for setting Accept-Language headers
The text was updated successfully, but these errors were encountered: