Skip to content

Instantly share code, notes, and snippets.

@marcobehlerjetbrains
Last active March 30, 2025 05:05
Show Gist options
  • Select an option

  • Save marcobehlerjetbrains/3b9ed46953f810160e3a47e81f88e027 to your computer and use it in GitHub Desktop.

Select an option

Save marcobehlerjetbrains/3b9ed46953f810160e3a47e81f88e027 to your computer and use it in GitHub Desktop.
Spring Boot REF2 - Http Post
(async function createPhoto() {
let photo = {"fileName": "hello.jpg"};
await fetch("http://localhost:8080/photoz", {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(photo)
})
.then(result => result.text())
.then(text => alert(text));
})();
@tommywong712
Copy link

As of April 29, 2024 I am also getting the 405 and can't find an answer.

same here

@WaterChick
Copy link

WaterChick commented Sep 4, 2024

The fetching code is correct, just update the mapping in your controller, to avoid this 405 (Method Not Allowed) Error:

You are not passing the actual photo variable, should be:

@PostMapping("/photoz")
public ResponseEntity<Photo> create(@RequestBody Photo photo) {
      photo.setId(UUID.randomUUID().toString());
      db.put(photo.getId(),  photo);
    return new ResponseEntity<>(photo, HttpStatus.CREATED);
}

@etienneauert
Copy link

The fetching code is correct, just update the mapping in your controller, to avoid this 405 (Method Not Allowed) Error:

You are not passing the actual photo variable, should be:

@PostMapping("/photoz")
public ResponseEntity<Photo> create(@RequestBody Photo photo) {
      photo.setId(UUID.randomUUID().toString());
      db.put(photo.getId(),  photo);
    return new ResponseEntity<>(photo, HttpStatus.CREATED);
}

Worked for me thank you

@MrSnor
Copy link

MrSnor commented Sep 14, 2024

@MrSnor
Copy link

MrSnor commented Sep 14, 2024

Adding a cors origin solves cross-origin issues in browsers -

@CrossOrigin(origins = "*")
@RestController
public class PhotozController {

}

@CentreMetre
Copy link

I was getting error 405 while the use the same JS in the gist, but it turned out i accidentally had
@PostMapping("/photos")
instead of
@PostMapping("/photoz")
(an s instead of a z in the post mapping path)
so if make sure you spelt that correct also.

@musafircodes
Copy link

@CentreMetre yes usually error 405 indicates somewhere you have gone incorrect in Mapping annotations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment