cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Can i send multiple post requests to an API endpoint and get the info if all succeeded ?

Layer
Visitor

Hello I am trying to send multiple post requests to an endpoint, i have a spark dataframe and each column of this dataframe is sent through the payload of the post request.

However when i run this in my notebook, no exception is raised. I'm guessing it is because the requests are executed on spark workers

That's why i wonder how can i get the info if a post request returned a HTTP error 422 or 502 ?

Here's something i tried but with no result

url = "string"
headers = {
    'header': 'XXXXX'
}

def process_partition(partition):
    partition_errors = [] 
    
    for row in partition:
        payload = {
            "col1": str(row["col1"]),
        }
        try:
            response = requests.post(url, json=payload, headers=headers)
            response.raise_for_status()
        except requests.exceptions.HTTPError as err:
            partition_errors.append({"error": str(err), "data": row.asDict()})
        except Exception as e:
            partition_errors.append({"error": str(e), "data": row.asDict()})
    
    return partition_errors

def log_errors(errors):
    if errors:
        for error in errors:
            print(f"Error: {error['error']}, Data: {error['data']}")
    else:
        print("All data has been sent.")

all_errors = []

def collect_partition_errors(partition):
    errors = process_partition(partition)
    all_errors.extend(errors) 

df_websocket_data.foreachPartition(collect_partition_errors)

log_errors(all_errors)

 

0 REPLIES 0

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group