Search code examples
pythonjsonpost

How to pass multiple words query to post request in python


trying to pass two words query to post request in python. Tried different patterns without success, the problem seems to be in this line, everything works great with single word query and should be working with two words:

query = "title:\"search this\""

Im getting this error:

{"error": {"expose": true, "statusCode": 400, "status": 400, "body": "\n{\n \"cat\": \"boats\",\n \"query\": \"title:\"search this\"\",\n \"size\": 5\n}\n", "cat": "entity.parse.failed"}} 

Here's the code:

query = "title:\"search this\""

form = cgi.FieldStorage()
n = form.getvalue('n')

data = f"""
{{
"cat": "boats",
"query": "{query}",
"size": {n}
}}
"""

Solution

  • Converting comment to answer.

    Query should look like this:

    query = 'title:\\"search this\\"'
    

    Then data becomes:

    {
    "cat": "boats",
    "query": "title:\"search this\"",
    "size": "test"
    }