0

My code in Python3:

import requests

URL = "http://xxx.xxx.com/querytext"

try:
    text = urllib.parse.quote_plus(text)
    r = requests.get(URL, params={"text": text}, auth=("guest", "1234"))
    r_json = r.json()
except Exception:
    return None

tag_results = r_json["data"]["result"]

My 'text' may contain some special characters so I want to do url encoding, as shown above. However, for one testing example, if I don't use this line:

text = urllib.parse.quote_plus(text)

I can get the expected result. Otherwise, I can't. So i suspect my way of using url encoding is wrong. What's wrong exactly?

One example of the text could be '#Thisisgreat#, yes!'

3
  • can you include some examples in your post? Commented Sep 15, 2021 at 3:53
  • stackoverflow.com/questions/40557606/… Commented Sep 15, 2021 at 3:56
  • @sh.seo how to use the encoder in this format: requests.get(URL, params={"text": text}, auth=("guest", "1234")) Commented Sep 15, 2021 at 3:59

2 Answers 2

1

Requests does the encoding for you, so you don't need to do it manually. So you should remove the line text = urllib.parse.quote_plus(text).

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

2 Comments

In the example, 'You can see that the URL has been correctly encoded by printing the URL:' there is no special characters. So I am not sure whether it needs additional encoding for special characters.
The encoding that Requests does includes encoding special characters, so there is no need to pre-encode strings that contain special characters.
0

As stated in the request docs (https://docs.python-requests.org/en/latest/):

Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your POST data. Keep-alive and HTTP connection pooling are 100% automatic, thanks to urllib3.

So you do not need to parse the text manually. Doing so may "double parse" it.

8 Comments

So you mean in my code above, i need to remove 'text = urllib.parse.quote_plus(text)'?
'or to form-encode your POST data', Is the requests POST or GET data?
I tried your code using requestbin.com without the parsing and it worked out. Are you having any issues not having it parsed manually?
You should look at 'There’s no need to manually add query strings to your URLs'. The params are the queries.
My http service didn't return expected results, so I am suspecting to add urlencoding. 'There’s no need to manually add query strings to your URLs', yes, but the question is whether the 'text' field in the params map needs to encoded or not?
|

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.