Support Questions

Find answers, ask questions, and share your expertise

how to get error response like 400 and 500 using InvokeHTTP processor in Apache NiFi?

avatar
Contributor

Hello sir,

I am using the InvokeHTTP processor in Apache NiFi to call an API. I am writing the response using the PutFile processor. When the API returns a 200 status code, it works fine and writes the response. However, when the API returns a 400 or 500 status code, it writes the original input file instead. I want to capture and write the actual response body for 400 or 500 status codes instead of the input file. here is process:

enam_0-1734415482731.png

here is InvokeHTTP processor:

enam_1-1734415773091.png

Here is putfile processor:

enam_2-1734415833246.png

if status 200 it write the response . I want write all response from api instead of input file if it has any status from api.

Please help me

1 REPLY 1

avatar
Master Mentor

@enam 
I see you have used the correct  InvokeHTTP processor which is used to send API requests and handle responses, including error codes 400, 500 etc.

Configuration for InvokeHTTP:

Set Required Properties:

To enable Error Responses add the following settings:

  • "Always Output Response": Set to true.
  • Output Response Attributes: Include attributes such as status.code and status.message to capture response metadata.
  • This ensures that the processor outputs responses regardless of status code.

Now route Responses Based on Status Code

Use the RouteOnAttribute processor to differentiate between success and error responses add two conditions:

  • Success Route: Check for status.code >= 200 AND status.code < 300.
  • Error Route: Check for status.code >= 400.

Connect the InvokeHTTP processor's Response relationship to the RouteOnAttribute processor.
To write Responses to Appropriate Locations

  • Use a PutFile or PutHDFS processor for both success and error routes:
  • Success Route:
    • Write successful responses to a specific directory.
  • Error Route:
    • Write error responses 400, 500 to a separate directory.

Include response details for debugging.

Spoiler
GenerateFlowFile --> InvokeHTTP --> RouteOnAttribute --> [Success] PutFile --> [Error] PutFile

Important Configuration Notes

  1. Configure Failure Handling in InvokeHTTP:

    • Connect the InvokeHTTP processor’s Original relationship to a LogMessage processor or another flow to avoid losing the original flowfile.
  2. Customize Filenames or Attributes:

    • Use UpdateAttribute to set filenames or directories dynamically based on attributes like status.code or status.message.
  3. Capture Full API Responses:

    • Ensure that Response Body from the API is written as the content of the output file.

Can you run the above flow and revert
Happy hadooping