What is the most appropriate HTTP status code to return if a required header is missing?

400 Bad Request It’s a user error in the request. Unlike with a 403, the client should be allowed to repeat their request, but only after modification: 10.4.1 400 Bad Request The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications. Edit As … Read more

Set UserAgent in http request

When creating your request use request.Header.Set(“key”, “value”): package main import ( “io” “log” “net/http” ) func main() { client := &http.Client{} req, err := http.NewRequest(“GET”, “http://httpbin.org/user-agent”, nil) if err != nil { log.Fatalln(err) } req.Header.Set(“User-Agent”, “Golang_Spider_Bot/3.0”) resp, err := client.Do(req) if err != nil { log.Fatalln(err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err … Read more

Does the order of headers in an HTTP response ever matter?

No, it does not matter for headers with different names. See RFC 2616, section 4.2: The order in which header fields with differing field names are received is not significant. However, it is “good practice” to send general-header fields first, followed by request-header or response- header fields, and ending with the entity-header fields. It DOES … Read more

What are Content-Language and Accept-Language?

Content-Language, an entity header, is used to describe the language(s) intended for the audience, so that it allows a user to differentiate according to the users’ own preferred language. Entity headers are used in both, HTTP requests and responses.1 Accept-Language, a request HTTP header, advertises which languages the client is able to understand, and which … Read more

HTTP 401 – what’s an appropriate WWW-Authenticate header value?

When indicating HTTP Basic Authentication we return something like: WWW-Authenticate: Basic realm=”myRealm” Whereas Basic is the scheme and the remainder is very much dependent on that scheme. In this case realm just provides the browser a literal that can be displayed to the user when prompting for the user id and password. You’re obviously not … Read more

Are Duplicate HTTP Response Headers acceptable?

Yes HTTP RFC2616 available here says: Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one “field-name: field-value” pair, without changing … Read more

What character encoding should I use for a HTTP header?

In short: Only ASCII is guaranteed to work. Some non-ASCII bytes are allowed for backwards compatibility, but are not supposed to be displayable. HTTPbis gave up and specified that in the headers there is no useful encoding besides ASCII: Historically, HTTP has allowed field content with text in the ISO-8859-1 charset [ISO-8859-1], supporting other charsets … Read more

what’s the difference between Expires and Cache-Control headers?

Cache-Control was introduced in HTTP/1.1 and offers more options than Expires. They can be used to accomplish the same thing but the data value for Expires is an HTTP date whereas Cache-Control max-age lets you specify a relative amount of time so you could specify “X hours after the page was requested”. HTML Cache control … Read more

What exactly does the Access-Control-Allow-Credentials header do?

By default, CORS does not include cookies on cross-origin requests. This is different from other cross-origin techniques such as JSON-P. JSON-P always includes cookies with the request, and this behavior can lead to a class of vulnerabilities called cross-site request forgery, or CSRF. In order to reduce the chance of CSRF vulnerabilities in CORS, CORS … Read more

Axios get access to response header fields

In case of CORS requests, browsers can only access the following response headers by default: Cache-Control Content-Language Content-Type Expires Last-Modified Pragma If you would like your client app to be able to access other headers, you need to set the Access-Control-Expose-Headers header on the server: Access-Control-Expose-Headers: Access-Token, Uid