diff options
| author | Thomas Voss <mail@thomasvoss.com> | 2025-11-03 12:26:03 +0100 | 
|---|---|---|
| committer | Thomas Voss <mail@thomasvoss.com> | 2025-11-03 12:26:03 +0100 | 
| commit | 6f65883c6ca833dc03e4c6f4ae28fae71681caff (patch) | |
| tree | 8d9fd79edf23800ddef36906391e902e58b00e20 /src | |
| parent | 33e94090b10c67ef036803dd14d47af749a5767a (diff) | |
Set User-Agent header when working with Wikipedia
Diffstat (limited to 'src')
| -rw-r--r-- | src/wikipedia/wikipedia.go | 16 | 
1 files changed, 14 insertions, 2 deletions
diff --git a/src/wikipedia/wikipedia.go b/src/wikipedia/wikipedia.go index e55e9da..bb14b82 100644 --- a/src/wikipedia/wikipedia.go +++ b/src/wikipedia/wikipedia.go @@ -42,13 +42,25 @@ func Init(locale string) error {  		}  		u.RawQuery = q.Encode() -		respjson, err := http.Get(u.String()) +		/* TODO: Use a context and NewRequestWithContext()? */ +		req, err := http.NewRequest("GET", u.String(), nil) +		if err != nil { +			return err +		} +		req.Header.Set("User-Agent", "euro-cash.eu/1.0.0 (admin@euro-cash.eu)") + +		respjson, err := http.DefaultClient.Do(req)  		if err != nil {  			return err  		}  		if respjson.StatusCode >= 400 &&  			respjson.StatusCode != http.StatusTooManyRequests { -			return fmt.Errorf("Failed to GET %s: %s", u, respjson.Status) +			msg := respjson.Status +			bytes, err := io.ReadAll(respjson.Body) +			if err == nil { +				msg = string(bytes) +			} +			return fmt.Errorf("Failed to GET %s: %s", u, msg)  		}  		defer respjson.Body.Close()  |