parserRequestHeader merges client and request headers, and sets headers automatically based on the request data. It also sets the User-Agent and Referer headers, and applies any cookies from the cookie jar.
(c *Client, req *Request)
| 120 | // parserRequestHeader merges client and request headers, and sets headers automatically based on the request data. |
| 121 | // It also sets the User-Agent and Referer headers, and applies any cookies from the cookie jar. |
| 122 | func parserRequestHeader(c *Client, req *Request) error { |
| 123 | // Set HTTP method. |
| 124 | req.RawRequest.Header.SetMethod(req.Method()) |
| 125 | |
| 126 | // Merge headers from the client. |
| 127 | for key, value := range c.header.All() { |
| 128 | req.RawRequest.Header.AddBytesKV(key, value) |
| 129 | } |
| 130 | |
| 131 | // Merge headers from the request. |
| 132 | for key, value := range req.header.All() { |
| 133 | req.RawRequest.Header.AddBytesKV(key, value) |
| 134 | } |
| 135 | |
| 136 | // Set Content-Type and Accept headers based on the request body type. |
| 137 | switch req.bodyType { |
| 138 | case jsonBody: |
| 139 | req.RawRequest.Header.SetContentType(applicationJSON) |
| 140 | req.RawRequest.Header.Set(headerAccept, applicationJSON) |
| 141 | case xmlBody: |
| 142 | req.RawRequest.Header.SetContentType(applicationXML) |
| 143 | case cborBody: |
| 144 | req.RawRequest.Header.SetContentType(applicationCBOR) |
| 145 | case formBody: |
| 146 | req.RawRequest.Header.SetContentType(applicationForm) |
| 147 | case filesBody: |
| 148 | req.RawRequest.Header.SetContentType(multipartFormData) |
| 149 | // If boundary is default, append a random string to it. |
| 150 | if req.boundary == boundary { |
| 151 | randStr, err := unsafeRandString(16) |
| 152 | if err != nil { |
| 153 | return fmt.Errorf("boundary generation: %w", err) |
| 154 | } |
| 155 | req.boundary += randStr |
| 156 | } |
| 157 | req.RawRequest.Header.SetMultipartFormBoundary(req.boundary) |
| 158 | default: |
| 159 | // noBody or rawBody do not require special handling here. |
| 160 | } |
| 161 | |
| 162 | // Set User-Agent header. |
| 163 | req.RawRequest.Header.SetUserAgent(defaultUserAgent) |
| 164 | if c.userAgent != "" { |
| 165 | req.RawRequest.Header.SetUserAgent(c.userAgent) |
| 166 | } |
| 167 | if req.userAgent != "" { |
| 168 | req.RawRequest.Header.SetUserAgent(req.userAgent) |
| 169 | } |
| 170 | |
| 171 | // Set Referer header. |
| 172 | req.RawRequest.Header.SetReferer(c.referer) |
| 173 | if req.referer != "" { |
| 174 | req.RawRequest.Header.SetReferer(req.referer) |
| 175 | } |
| 176 | |
| 177 | // Set cookies from the cookie jar if available. |
| 178 | if c.cookieJar != nil { |
| 179 | c.cookieJar.dumpCookiesToReq(req.RawRequest) |