1

I am currently working on a tutorial to get to know Spring Boot and currently facing the following problem.

During my registration process (works correctly -> user ends up in the database) I get the status code 200/OK in the browser console, but also an error message regarding an incorrect syntax:

enter image description here

enter image description here

My backend code looks like this:

AuthController:

@RestController
@RequestMapping(value = "/api/auth")
@AllArgsConstructor
public class AuthController {

    private final AuthService authService;
    private final RefreshTokenService refreshTokenService;

    @PostMapping(value = "/signup")
    public ResponseEntity<String> signup(@RequestBody RegisterRequest registerRequest) {
        /*
        * RegisterRequest: Through this class we are transferring the user details like username, password and email as part of the RequestBody (DTO)
        * */
        authService.signUp(registerRequest);

        return new ResponseEntity<>("Registration Successful", null, OK);
    }
 ....

AuthService:

  @Transactional
  public void signUp(RegisterRequest registerRequest) {
    User user = new User();

    user.setUsername(registerRequest.getUsername());
    user.setEmail(registerRequest.getEmail());
    user.setPassword(passwordEncoder.encode(registerRequest.getPassword()));
    user.setCreated(now());
    user.setEnabled(false);

    userRepository.save(user);

    String token = generateVerificationToken(user);

    String message  = mailContentBuilder.build("Thank you for signing up to Spring Reddit, please click on the below url to activate your account : "
            + ACTIVATION_EMAIL + "/" + token);

    mailService.sendMail(new NotificationEmail("Please Activate your account", user.get

Email(), message));
    }

Used DTO:

public class RegisterRequest {
    private String email;
    private String username;
    private String password;
}

My frontend code looks like:

SignUpComponent:

  signUp() {
    this.signUpRequestPayload.email = this.signUpForm.get('email').value;
    this.signUpRequestPayload.password = this.signUpForm.get('password').value;
    this.signUpRequestPayload.username = this.signUpForm.get('username').value;

    this.authService.signUp(this.signUpRequestPayload).subscribe((data) => {
      console.log('Sign up successful', data);
    });
  }

AuthService:

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  private headers = new HttpHeaders({ 'Content-Type': 'application/json' });

  constructor(private http: HttpClient) { }

  signUp(signUpRequestPayload: SignUpRequestPayload): Observable<SignUpRequestPayload> {
    const body = JSON.stringify(signUpRequestPayload);
    return this.http.post<SignUpRequestPayload>('http://localhost:8080/api/auth/signup', body, { headers: this.headers });
  }
}

Used interface:

export class SignUpRequestPayload {
  email: string;
  password: string;
  username: string;
}

What am I doing wrong here?

2 Answers 2

1

I solved it like this:

  signUp(signUpRequestPayload: SignUpRequestPayload): Observable<string> {
    const body = signUpRequestPayload;
    return this.http.post('http://localhost:8080/api/auth/signup', body, { responseType: 'text', headers: this.headers });
  }

I had to remove from the post method and set the responseType to 'text'. I also had to remove the JSON.stringify() method and set the return type to Observable.

Sign up to request clarification or add additional context in comments.

Comments

0

As your response("Registration Successful") is not valid JSON.

So please remove <SignUpRequestPayload> from below line

return this.http.post<SignUpRequestPayload>

2 Comments

It depends on your response type, you can remove it to use string at this moment.
I tried to remove it completely and to replace the type with string --> Same error..

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.