4

I consider using fluent-http in a project.

I started with a simple "login/password" page. I create a simple POJO with fields login and password :

public class LoginRequest() {
  private String login;
  private String password;
  //...
}

And I send it to fluent-http via a Resource :

@Prefix("/user")
public class PersonResource {

  @Post("/")
  public String get(LoginRequest loginRequest) {
    //[...]
  }
}

And it works well :)

Now, I wondered if it was possible to send a response with code HTTP 200 in case of success and code HTTP 401 in case of failure.

So I tried to inject the Response :

@Post("/")
public String login(LoginRequest loginRequest, Response response) {
  if(loginRequest.getPassword().equals("helloworld")) {
    response.setStatus(200);
    return "SUCCESS";
  } else {
    response.setStatus(401);
    return "ERROR";
  }
}

The correct String is returned but the status code does not seem to be used. In both cases, the response has a code HTTP 200.

Note : I found that some status code are pre-implemented :

  • In case of exception, a code 500 is returned.
  • In case of resource not found, a code 400 is returned.

Any idea?

1 Answer 1

1

If you want to change the default content-type, status or headers, the method should return a net.codestory.http.payload.Payload.

Here's what you should write:

@Post("/")
public Payload login(LoginRequest loginRequest) {
  if(!loginRequest.getPassword().equals("helloworld")) {
    return new Payload("ERROR").withCode(HttpStatus.UNAUTHORIZED);
  }

  return new Payload("SUCCESS").withCode(HttpStatus.CREATED);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.