Search code examples
pythonpython-requestspytestweb-api-testing

Passing input to URL path parameter from variable - Python


Please someone let me know how I could send input to URL path parameter from a variable. I do have an idea on passing input to Query Parameter through payload but below API uses path parameter and I need to pass 8, 3 and + as variables and I do not have an idea how I could pass them:

http://localhost:8184/messenger/webapi/Calculator/8/3/+

Solution

  • You could just use string interpolation to build a URL. Do make sure you quote your values before putting them in the URL:

    from urllib import quote
    
    op1, op2, operator = '8', '3', '+'
    url = 'http://localhost:8184/messenger/webapi/Calculator/{}/{}/{}'.format(
        quote(op1), quote(op2), quote(operator))