Json_parse_exception - Unexpected character (''' (code 39)): was expecting double-quote to start field name

I have a curl request which is:

curl -XGET "http://localhost:9200/_search" -H 'Content-Type: application/json' -d' { "query": { "range": { "created": { "gte": "2016-01-01", "lte": "2018-01-01", "format": "yyyy-MM-dd" } } } }'

I need to translate this to a python request.
I have tried something like:

 query = {
            "query": {
                "range": {
                    "created": {
                    "gte": start_date,
                    "lte": end_date,
                    "format": "yyyy-MM-dd"
                    }
                }
            }
        }

        response = requests.post(
            url=settings.ES_SERVER_URL + SEARCH_URL,
            headers={'Content-Type': 'application/json'},
            data=str(query),
        )

I get:

[
    "{\"error\":{\"root_cause\":[{\"type\":\"json_parse_exception\",\"reason\":\"Unexpected character (''' (code 39)): was expecting double-quot",
    "e to start field name\\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@4c81d054; line: 1, column: 3]\"}],\"type",
    "\":\"json_parse_exception\",\"reason\":\"Unexpected character (''' (code 39)): was expecting double-quote to start field name\\n at [So",
    "urce: org.elasticsearch.transport.netty4.ByteBufStreamInput@4c81d054; line: 1, column: 3]\"},\"status\":500}"
]

What am I doing wrong?

str(query) won't produce a valid JSON string. Try json.dumps from the JSON lib instead.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.