Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Calling Status code using JSON Response.

Avatar

Level 10

Hi,

We hard-coded the Status codes and messages like ex: 200(Success), 403(Forbidden), 404(Nt found). etc., Now we have to call the Status codes through JSON response.

Can anyone tell me how to do this and any code snippet will be helpful to me.

Thanks,
Ratna Kumar.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Ratna Kumar wrote...

Hi,

We hard-coded the Status codes and messages like ex: 200(Success), 403(Forbidden), 404(Nt found). etc., Now we have to call the Status codes through JSON response.

Can anyone tell me how to do this and any code snippet will be helpful to me.

Thanks,
Ratna Kumar.

 

The issue has been resolved.

View solution in original post

3 Replies

Avatar

Level 4

Did you hard code the response code into JSON?

Avatar

Administrator

Hi Ratna,

I am not very clear what exactly you mean by "We hard-coded the Status codes and messages " and "to call the Status codes through JSON response."

If the question is how to extract HTTP Status code, then Use the RestTemplate#exchange(..) methods that return a ResponseEntity. This gives you access to the status line and headers (and the body obviously).

Or Use HttpURLConnection:

//

URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

int code = connection.getResponseCode();

 

Add the status code to Json before returning. 

Reference Article:- http://stackoverflow.com/questions/4687271/jax-rs-how-to-return-json-and-http-status-code-together

I hope this will help you.

 

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Correct answer by
Level 10

Ratna Kumar wrote...

Hi,

We hard-coded the Status codes and messages like ex: 200(Success), 403(Forbidden), 404(Nt found). etc., Now we have to call the Status codes through JSON response.

Can anyone tell me how to do this and any code snippet will be helpful to me.

Thanks,
Ratna Kumar.

 

The issue has been resolved.