Azure Function Ouput to ADF variable

Jatinder Luthra 130 Reputation points
2023-10-12T23:41:57.7233333+00:00

Hello folks,

I am testing Azure Function app in ADF pipeline. Based on input I am receiving to Azure python function, need to do some processings and write the output to variable, which will be input to next step.

Here is my test code:

@app.route(route="http_trigger_2")
def http_trigger_2(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        text =req.params.get('name')
        d = p.DataFrame({'Data': [text], 'Size': [len(text)]})
        res = d.to_json(orient='records')
        print(res)
        return func.HttpResponse(res, mimetype='application/json')
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

When I trigger the function using url in browser passing value for name, it executes fine and give output in browser as below

[{"Data":"Testing","Size":7}]

But while POST this function in ADF pipeline with body, getting error as below:

Operation on target Azure Function1 failed: Call to provided Azure function 'http_trigger_2' failed with status-'InternalServerError' while invoking 'POST' on 'https://func-app.azurewebsites.net' and message - 'Invoking Azure function failed with HttpStatusCode - InternalServerError.'.

I believe it might be related to way output is returned.

Any comments or suggestions are much appreciated.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,624 questions
{count} votes

Accepted answer
  1. VenkateshDodda-MSFT 24,951 Reputation points Microsoft Employee Moderator
    2023-10-17T09:40:54.8333333+00:00

    @Jatinder Luthra Thanks for your patience on this.

    Post making the below changes to the above shared code, I am able to execute the function from portal and from the data factory pipelines without any issues.

    Removed this line text =req.params.get('name') and updated this line : d = p.DataFrame({'Data': [name], 'Size': [len(name)]})

    Here is the modified code:

    import logging
    import azure.functions as func
    import pandas as p 
    
    from azure.functions import HttpRequest, HttpResponse
    
    
    def main(req: HttpRequest) -> HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
    
        
        name = req.params.get('name')
        if not name:
            try:
                req_body = req.get_json()
            except ValueError:
                pass
            else:
                name = req_body.get('name')
    
        if name:
            d = p.DataFrame({'Data': [name], 'Size': [len(name)]})
            res = d.to_json(orient='records')
            print(res)
            return func.HttpResponse(res, mimetype='application/json')
        else:
            return func.HttpResponse(
                 "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
                 status_code=200
            )
    

    Here are the sample outputs for your reference.

    1. Output from Code+Test in portal.

    enter image description here

    1. Output from ADF pipeline Azure Function activity:

    enter image description here

    Feel free to reach back to me if you have any further questions on this.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.