question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add HTTP User-Agent header to API calls

See original GitHub issue

According to the MediaWiki API docs, there should be an HTTP User-Agent header added to API calls. We should follow the policy by adding a user agent to our HTTP GET calls.

Probably something like the following should do it (reference):

import wikipron
import requests


HTTP_HEADERS = {
    "User-Agent": (
        f"WikiPron/{wikipron.__version__} "
        "(https://github.com/kylebgorman/wikipron) "
        f"requests/{requests.__version__}"
    ),
}

requests.get(..., headers=HTTP_HEADERS)

I think there are only two places in the codebase where a requests.get call (or equivalent) is made:

https://github.com/kylebgorman/wikipron/blob/fab40759d4fc95579769f44a9decf1c087771e75/wikipron/scrape.py#L44

https://github.com/kylebgorman/wikipron/blob/fab40759d4fc95579769f44a9decf1c087771e75/wikipron/scrape.py#L63

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jacksonlleecommented, Oct 14, 2020

Hi @Alireza-Sampour , thank you for your interest in contributing! I just found two more spots – I forgot about tests/:

https://github.com/kylebgorman/wikipron/blob/fab40759d4fc95579769f44a9decf1c087771e75/tests/test_wikipron/test_config.py#L193

https://github.com/kylebgorman/wikipron/blob/fab40759d4fc95579769f44a9decf1c087771e75/tests/test_wikipron/__init__.py#L16

We can probably define the HTTP_HEADERS dict just once, in wikipron/srape.py. Then we can use it directly for the first and second locations I’ve mentioned above. For the third case in tests/test_wikipron/test_config.py, HTTP_HEADERS could be imported through this line. For the fourth case in tests/test_wikipron/__init__.py, I believe we could do from wikipron.scrape import HTTP_HEADERS just before this line – I was thinking through and trying to see if we’d run into surprises like circular imports, ending up typing all this out…

Here are the notes for contributors.

Read more comments on GitHub >

github_iconTop Results From Across the Web

User-Agent - HTTP - MDN Web Docs - Mozilla
The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, ...
Read more >
HTTP headers | User-Agent - GeeksforGeeks
The HTTP headers User-Agent is a request header that allows a characteristic string that allows network protocol peers to identify the ...
Read more >
RESTful API: require user agent string? - Stack Overflow
Since the User-Agent header is not absolutely required to be present within HTTP requests (the spec says the header SHOULD be there, ...
Read more >
Set a custom user agent | New Relic Documentation
When using New Relic's RESTful APIs, set a User-Agent header that identifies your integration in API calls. This custom User-Agent string allows New...
Read more >
How to add User-Agent header to HttpClient in .NET
This article will introduce you to the format for the User-Agent HTTP header as well as how to add it in various ways...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found