Support Questions

Find answers, ask questions, and share your expertise

Send JSON response from HandleHTTPRequest processor

avatar
New Contributor

I am modifying the standard *HandleHTTPRequest* Processor in NiFi to add Oauth2 authentication. The idea is to accept scopes and client_id as part of the processor setup and validate the incoming JWT token against these scopes and client_id.

i am trying to send a JSON Response when the scope or client_id is incorrect as below

 

 

String newContent = "{ \"message\" : \"Unauthorized\" }";
container.getResponse().setStatus(HttpServletResponse.SC_UNAUTHORIZED);
container.getResponse().setContentType("application/json");
container.getResponse().getWriter().write(newContent);
return;

 

 

the request is hung and times out. Is this because HandleHttpRequest can't send the response back and I have to use HandleHTTPResponse processor to process the response?

The below code sends a HTML Response which I want to change to a proper JSON.

 

 

container.getResponse().sendError(HttpServletResponse.SC_UNAUTHORIZED,newContent);

 

1 REPLY 1

avatar
Super Guru

@Vamshi245  Yes,  HandleHttpRequest and HandleHttpResponse are used in tandem.  Behind the processors is a map cache which holds connection session between request/response processors.   If your flowfile (json) coming out of custom HandleHttpRequest is delivered to stock HandleHttpResponse, it will send the json to the original connecting client.

 

If this answer resolves your issue or allows you to move forward, please choose to ACCEPT this solution and close this topic. If you have further dialogue on this topic please comment here or feel free to private message me. If you have new questions related to your Use Case please create separate topic and feel free to tag me in your post.

 

Thanks,

Steven