How do I send multiple requests to the same URL (same API) in Python?

Asked

Viewed 41 times

0

I’m calling an API that creates table in Hive. I’m developing an automated test script to make 30 requests and be able to create 30 tables in Hive. For this, I put a logic in which the tablename for each request is tableName 0, tableName 1 and so on (I did with range).

However, when I run the function, only the first table is inserted in the Hive (name_table_0) and returns status_code 200. The request of the others returns status_code 200 but the tables are not created in Hive.

Does anyone have suggestions to actually get other requests met and tables created?

def tests_requests_loops(table_sufix: str):

      nome_tabela = f"nome_tabela_{table_sufix}"

      body = {
        "name_table": nome_tabela,
        "schema_table": "{\"name\": \"Value\", \"type\": \"record\", \"namespace\": \"com.demo.producer.avro\", \"fields\": [{\"name\": \"id\", \"type\": \"int\"}, {\"name\": \"first_name\", \"type\": [\"null\", \"string\"]}",
        "location":"hive-desenvolvimento"
      }
      print(body)

      url = "https://api-dev-cloud/development/ingestion-events/configuracoes"

      response = requests.request("POST", url, data=json.dumps(body), verify=False)
      print(response)

for i in range(30):
      tests_requests_loops(table_sufix=i)

Obs: has the parameters of headers but removed because it did not make much difference to the question.

  • The code seems correct. The first request also returns 200?

  • That, the first also returns 200.

  • 1

    It should work. You can create a MOCK API to intercept requests and see if it’s sending everything correctly. If yes, the problem is not in your code.

  • Thank you, Woss. I made some mocks, the code is correct. Hive was not accepting the same Location for the same requests, so I had to put this parameter in the range as well. Anyone who wants to make multiple requests to the same URL, this is a simple way but that solves the problem.

1 answer

0

I believe your problem is not in the concept of "sending multiple requests". But have you stopped to validate this python dictionary mixed with string json.... In which you still convert to json, when sending post? If you had created a dictionary and then converted, you would have seen the syntax error... Or another idea would be to use the parameter json who already performs this conversion.

def tests_requests_loops(table_sufix):

    body = {
            "name_table": f"nome_tabela_{table_sufix}",
            "schema_table": {
                    "name": "Value",
                    "type": "record",
                    "namespace": "com.demo.producer.avro",
                    "fields": [
                            {
                                "name": "id",
                                "type": "int"
                            },
                            {
                                "name": "first_name",
                                "type": ["null", "string"]
                            },
                    ]
            }
    }

    url = "https://api-dev-cloud/development/ingestion-events/configuracoes"
    response = requests.request("POST", url, json=body, verify=False)
    return response

Browser other questions tagged

You are not signed in. Login or sign up in order to post.