Getting HTML response while querying the data from the InfluxDB using the Python

I am trying to fetch the data from the InfluxDB using the Python InfluxDB-client library but instead of the expected output, I am getting HTML in response.

I am putting the code here.

def get_config(deployment, org=None):
    with open('./configs/config.json', 'r') as f:
        config = json.load(f)
        
        return config[deployment][org]

    
try: 
    config = get_config(deployment, org)

    bucket = config['bucket']
    org = config['org']
    token = config['token']
    # Store the URL of your InfluxDB instance
    url = config['url']

    logger.info("query started")
    print(url, token, org)
    client = influxdb_client.InfluxDBClient(
            url=url,
            token=token,
            org=org,
            timeout=3600*1000 # 30 mins
        )
    
    query_api = client.query_api()

    logger.info("query executing")

    query = f'inst_table = from(bucket: "bucket_name")\
        |> range(start: {{start_time}}, stop: {{end_time}})\
        |> filter(fn: (r) => r["_measurement"] == "cu_om")\
        |> filter(fn: (r) => r["tag_name"] !~ /^$/)\
        |> filter(fn: (r) => r["tag_name2"] == "tag_value")\
        |> filter(fn: (r) => r["_field"] =~ /^FieldName$)\
        |> filter(fn: (r) => r._value > 0)\
        |> mean()\
        |> duplicate(column: "_stop", as: "_time")\
        |> keep(columns: ["_time","_value","_field","tag_name2","tag_name"])\
        |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")\
        |> yield(name:"output")'
    
    query = query.format(start_time='2024-04-22T23:45:00Z',end_time='2024-04-23T23:45:00Z')
    print(query)
    csv_record = query_api.query_csv(org=org, query=query)
    data = [row for row in csv_record]
    print(data)

I have verified token, URL, and org is correct. The query is also correct after replacing the start and stop time I am able to see the data inside the InfluxDB but I am not able to see the data using the python code.

Below output I am getting.

[['<!doctype HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width', 'initial-scale=1">
<meta name="description" content="InfluxDB is a time series platform', ' purpose-built by InfluxData for storing metrics and events', ' provides real-time visibility into stacks', ' sensors', ' and systems.">
<title>InfluxDB</title>
<base href="/"><link rel="shortcut icon" href="/favicon.ico">
</head>
<body>
<div id="react-root" data-basepath=""></div>
<script src="/6588f709b0.js"></script></body></html>']]

I am using influxdb-client==1.41.0 for Python.

Hello @Ravikant_Gautam,
Please use this client library:

With latest being 1.42

You’ll also need to iterate through your results not only the rows like you’ve done but also the cells.

For this reason I highly recommend using pandas and then converting to CSV if you need after.

from influxdb_client import InfluxDBClient, Point, Dialect
from influxdb_client.client.write_api import SYNCHRONOUS

client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org")

write_api = client.write_api(write_options=SYNCHRONOUS)
query_api = client.query_api()

"""
Prepare data
"""

_point1 = Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
_point2 = Point("my_measurement").tag("location", "New York").field("temperature", 24.3)

write_api.write(bucket="my-bucket", record=[_point1, _point2])

"""
Query: using Pandas DataFrame
"""
data_frame = query_api.query_data_frame('from(bucket:"my-bucket") '
                                        '|> range(start: -10m) '
                                        '|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value") '
                                        '|> keep(columns: ["location", "temperature"])')
print(data_frame.to_string())

"""
Close client
"""
client.close()

You can use the .format method but I would encourage you to just try a plain query first.
Also are you able to return data with cURL? For verification?

Thanks!