0

I need to avoid sending a note as a request param in a Post operation. To do so I need to move it from the url to the body but I don't know how.

Here is the code:

@PostMapping(value = "/person/{cuco_id}/notes/{typeNote}/**{note}**", produces = { "application/json; charset=UTF-8" }, consumes = {
        "application/json; charset=UTF-8" })
@ApiOperation(value = "Create a new note for the specified Cuco ID", response = Note.class)

@Transactional
public ResponseEntity<Note> createNote(
        @ApiParam(value = "Cuco ID for which the note should be created", required = true) @PathVariable Long cuco_id,
        @ApiParam(value = "Type of note which should be created", required = true) @PathVariable String typeNote,
        @ApiParam(value = "Note which should be added to the Cuco ID", required = true) @PathVariable String note,
        @AuthenticationPrincipal Principal principal)
        throws ElementAlreadyExistsException, ValidationErrorException, IOException {

    // check if Note entity for cuco id exists
    Integer seq = repo.findNextSeq(cuco_id,typeNote);

    NotePK notePk = new NotePK(cuco_id, seq, typeNote);
    Note coreEntity = new Note();

    Optional<Note> optNote = repo.findById(notePk);
    
    if (!optNote.isPresent()) {

        coreEntity.setIdCuco(notePk.getIdCuco());
        coreEntity.setSeq(seq);
        coreEntity.setTypeNote(notePk.getTypeNote());
        coreEntity.setNote(note);
        coreEntity.setCreationDate(new Date());
        coreEntity.setLastModDate(new Date());
        coreEntity.setCreationBy(principal.getName());
        coreEntity.setLastModBy(principal.getName());

    } else {
        // If it already exits, do nothing
        throw new ElementAlreadyExistsException("The note already exists");
    }
DiegoMG
  • 383
  • 1
  • 4
  • 18
  • 1
    If you don't want to send as a request param you can send it as a RequestBody. So you would have something like this: `createNote(... @RequestBody Note note, @AuthenticationPrincipal Principal principal)` Pass the note to the request body like a JSON object – Zoe Lubanza Jun 01 '22 at 15:17
  • Hello Zoe! Thanks, should I remove it from the URL as well or no? – DiegoMG Jun 01 '22 at 15:34
  • yes it needs to be removed from the URL as well. What fields do you have in your **Note** class? – Zoe Lubanza Jun 01 '22 at 16:46
  • The content is just a PK and a string field for the note. – DiegoMG Jun 02 '22 at 07:43

1 Answers1

1

Now when trying os Postman I get this error message: The request was rejected because the URL contained a potentially malicious String "//"

But I can't find any //

enter image description here

DiegoMG
  • 383
  • 1
  • 4
  • 18