1

I am working on a REST API and using python. say for a get request ( sample below), I am assuming , anyone who makes a call will URL encode the URL, what is the correct way to decode and read query parameters in python?

'https://someurl.com/query_string_params?id=1&type=abc'

import requests
import urllib

  def get():
        
    //parse query string parameters here
        
3

1 Answer 1

2

Here's an example of how to split a URL and get the query parameters:

import urllib.parse

url='https://someurl.com/query_string_params?id=1&type=abc'

url_parts = urllib.parse.urlparse(url)

print( f"{url_parts=}" )

query_parts = urllib.parse.parse_qs(url_parts.query)

print( f"{query_parts=}" )

Result:

url_parts=ParseResult(scheme='https', netloc='someurl.com', path='/query_string_params', params='', query='id=1&type=abc', fragment='')
query_parts={'id': ['1'], 'type': ['abc']}

Documentation is here https://docs.python.org/3/library/urllib.parse.html?highlight=url%20decode

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.