-1

I am building a python (3.6) function that will work as a microservice that expects the payload to be a url string. I use validators.url to check if the string passed is like a URL before executing the function which includes requests.get call to get the website at the URL.

The problem occurs if the url string passed is itself wrapped in single or double quotes. In that case the function fails.

so:

localohost:1234/api/Function?url=https://www.google.com works but localohost:1234/api/Function?url='https://www.google.com' and localohost:1234/api/Function?url="https://www.google.com" both fail to execute both because they are not like URL and because requests.get cannot interpret the string. This is an edge case, but one I'd love to handle properly.

Is there a way to check for a string with "doubled up quotation marks" (as in '"string"' or "'string'") in Python and remove them?

Thanks

dozyaustin
  • 611
  • 1
  • 6
  • 20
  • Failing seems like completely the right thing to do. Properly speaking your URLs should also have a schema (`http://` or `https://` or etc). – tripleee Feb 06 '20 at 13:41
  • ok my example is wrong .. they do have ```http://``` or ```https://``` .. that is the issue. they do not fail due to missing http(s) , but due to extra qutation marks, as outlined in the question. I will edit the question to clarify this. – dozyaustin Feb 06 '20 at 13:44
  • Please [edit] to clarify what you have already searched for and why what you found was not helpful. – tripleee Feb 06 '20 at 13:46
  • I searched extensively, found no answer that astisfies this problem. Likely because 'doubled up quotation marks' is a term that makes sense to me, but is not what others call it .. so my queries are failing. – dozyaustin Feb 06 '20 at 13:48
  • Duplicate of https://stackoverflow.com/questions/40950791/remove-quotes-from-string-in-python/40950987 – tripleee Feb 06 '20 at 13:49
  • I don't see any doubled quotes. There is simply one pair of quotes around the URL in your examples. – tripleee Feb 06 '20 at 13:50

1 Answers1

1

You can use the strip function:

quoted   = "'url'"
unquoted = quoted.strip("'").strip('"')

If you want to make sure that striping is balanced, you could make your own function:

def removeQuotes(s):
    if s and s[0] == s[-1] and s[0] in ["'",'"']: 
         return removeQuotes(s[1:-1])
    return s
Alain T.
  • 40,517
  • 4
  • 31
  • 51