DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From 24 Hours to 2 Hours: How We Fixed a Broken BI System With Apache Airflow
  • Developing a Nationwide Real-Time Telemetry Analytics Platform Using Google Cloud Platform and Apache Airflow
  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  • Building Scalable and Resilient Data Pipelines With Apache Airflow

Trending

  • Genkit Flows as senro Steps: Extremely Powerful AI Pipelines in Go
  • Stop Paying a Model to Make Decisions You Already Made
  • Kubernetes Operations Playbook: The Essentials for Keeping Scale, Complexity, and Drift Under Control
  • Rebuilding Elasticsearch Under Load: Unsafe Watermarks and a Sampling Blind Spot

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

SimpleHTTPOperator in Apache Airflow

This tutorial covers how to call a REST service using SimpleHTTPOperator, the configuration of http_conn_id through Airflow UI, and the output of DAG execution.

By 
Sameer Shukla user avatar
Sameer Shukla
DZone Core CORE ·
Feb. 01, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
22.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Introduction

The SimpleHTTPOperator is an Operator in Airflow which can be used to request an API, say any REST service, and get the response from the Service. This article covers the important aspects of using a SimpleHTTPOperator, such as: 

  • Where to configure the URL (http_conn_id)
  • How to invoke a service
  • How to parse the response back from the service
  • Other attributes

Let’s explore! 

SimpleHTTPOperator

As earlier mentioned, SimpleHTTPOperator is used to call an API by sending an HTTP request. The example in the article is going to invoke an open source weather API, which is a GET call. 

Dallas weather example

One can see in the image {city} is replaced by a valid city “Dallas.” The response received is in JSON format, which explains the weather in Dallas. We are going to invoke the same request through SimpleHTTPOperator. 

As we know, the first step of the workflow execution in Airflow is to create a DAG. Then, the Operator should be used for the task execution.

Operator task execution

The DAG with the name “weatherServiceCall” is created with standard attributes like start_date and schedule_interval. The schedule_interval is defined using a cron-expression, which will invoke the service every 5 minutes. 

Then the SimpleHTTPOperator task is created, which has the following attributes: 

  • task_id = “weatherapi” is the task id
  • method = Indicates the HTTP Verb, which is a GET call 
  • http_conn_id = Explained briefly in the latter part of the article
  • endpoint = The relative part of the URL ex: /weather/Dallas
  • headers = Indicates HTTP Headers 
  • response_check = The lambda/user-defined function for handling the response from the REST Service

Configuring http_conn_id

The “http_conn_id” reflects the HTTP connection/REST endpoint details for the SimpleHTTPOperator. In our example, we have used a string “weather_api” which is configured through the Airflow UI. On the Airflow UI, go to the Admin section and click on Connections.

Admin section of Airflow UI

On the "Add Connection" screen, configure the details such as Conn Id (where the name “weather_api” is configured), Conn Type (which is HTTP), and the Host details.

Add connection

Click on Save, and that’s it: http_conn_id is configured!

response_check

In the example, we have used the Python function “handle_response” for handling the HTTP response. The implementation of the function is:

Handle_response

Implementation is straightforward. In the case the status code is 200, we are returning True. Otherwise, we would return False. 

DAG Details After Execution

DAG Details After Execution

Logs 

The logs showcase that the response received is successful, and the print statement of the handle_response function is displayed. 

Log showing successful response received

Summary

The article explains in detail how to call a REST service using SimpleHTTPOperator. We also covered the configuration of http_conn_id through Airflow UI as well as showcased the output of DAG execution.

Apache Airflow

Opinions expressed by DZone contributors are their own.

Related

  • From 24 Hours to 2 Hours: How We Fixed a Broken BI System With Apache Airflow
  • Developing a Nationwide Real-Time Telemetry Analytics Platform Using Google Cloud Platform and Apache Airflow
  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  • Building Scalable and Resilient Data Pipelines With Apache Airflow

Partner Resources

×

Comments

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook