How to pull xCom in HttpSensor operator

Viewed 1753

I want to call an API, which needs data of the previous task which I saved in xCom.How to access that xCom.

I am using HttpOperater as well as HttpSensor for calling API.

2 Answers

you need to use the xcom_pull method on the context:

http_task = SimpleHttpOperator(
    task_id='http_call',
    endpoint='nodes/url',
    data="name=Joe",
    headers={"Content-Type": "application/x-www-form-urlencoded"},
    dag=dag,
)

def get_http_payload(**context):
    http_payload = ['ti'].xcom_pull(task_ids='http_call')
    print(http_payload)

process_output = PythonOperator(
    task_id='process_stuff',
    python_callable=get_http_payload,
    provide_context=True,
    dag=dag,
)

http_task >> process_output

You can use templating in HttpSensor which has two template parameters.

Example :

HttpSensor(task_id='sfcase_hook_sensor',
                http_conn_id='conn_id', 
                method="GET",
                endpoint= + "xyz.com/abc/{{ti.xcom_pull(task_ids='previous_task_id')}}",
                request_params={
                   "someParam" : "{{ti.xcom_pull(task_ids='previous_task_id')}}"
                },
                extra_options={
                    'verify': False
                },
                headers={
                    "access_token" : token
                },
                response_check=hook_check, xcom_push=True)
Related