2

I got some strange error with requests. Code is:

    key = key.replace(" ", "_")
    print(key)
    url = f"https://pl.wikipedia.org/api/rest_v1/page/summary/{key}"
    print(url)
    summary = http.get(url)
    summary = summary.json()
    print(summary)

The output from prints:

A

https://pl.wikipedia.org/api/rest_v1/page/summary/A


{'type': 'https://mediawiki.org/wiki/HyperSwitch/errors/bad_request', 'method': 'get', 'detail': 'title-invalid-characters', 'uri': '/pl.wikipedia.org/v1/page/summary/A%0A'}

The site returns an error with uri that is different from the url variable. Why? Anyway, I cannot access it. And I see A is changing to A%0A.

2
  • What's the content of key at the beginning? Commented Nov 20, 2020 at 8:38
  • It is user-defined. Commented Nov 20, 2020 at 10:15

1 Answer 1

4

Can you see an empty line in the output (2nd line) after the A. That is the problem. A line feed is there at the end of A.. (A\n). You have to remove the \n.

Use key.strip(). This will remove all leading and trailing whitespace.

And URL encoding for LF - line feed (\n) is %0A. That's why your URL changed to A + %0A.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.