0

I am processing data using GetMapping in Spring Boot.

I looked up the reason and it is expected that when I call Get() in Axios, the Body data does not exist at all and I am passing it. (not empty e.g { } )

Is there any way to handle this ?

When calling json through postman, the result is as follows. postman Get (content-type : Json)

set :

@RequestMapping(value = "/", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

success case :

{
  //data null
}
response : {
  status : 200
} 

fail case :

//{  --> remove brace (I didn't actually comment it out, I used it to indicate that I've removed it.)
    //data null
//}

response : 
{
  "statusCode": "ERROR",
  "status": 415,
  "message": "Content type '' not supported",
  "detailMessage": null
}
Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    "Yes, you can send a request body with GET but it should not have any meaning." https://stackoverflow.com/q/978061/592355 – xerx593 Jan 11 '22 at 23:04
  • 1
    Does this answer your question? [HTTP GET with request body](https://stackoverflow.com/questions/978061/http-get-with-request-body) – Phil Jan 11 '22 at 23:06
  • so if you want to send json: put/patch/post (/...) it! ..if you want to (send data with) GET: (then,) please with (uri/path) parameters only. – xerx593 Jan 11 '22 at 23:09
  • a "workaraund" (let's say "hack"): we can send json as (url-encoded) uri (string) parameter... which, we can map (back-end's) to json(/object)... – xerx593 Jan 11 '22 at 23:13
  • i'm not good at english sorry. To explain it simply, when sending a get message from a tool such as postman, a json format like { "aaa":"bbb", "ccc":ddd"} is allowed, and empty data like { //empty } is also allowed. I think axios will throw an error if you pass it without braces. In summary, I want to put the body somehow when using the axios get() method. e.g) { ' ' } –  Jan 11 '22 at 23:19
  • The problem seems to be that there is no body when sending a Get message. –  Jan 11 '22 at 23:23
  • "Yes, you can send a request body with GET but it should not have any meaning." means: DON't ! ..at least not with HTTP <= 1.1(2014) ;) – xerx593 Jan 12 '22 at 02:37
  • neither with http2 (https://greenbytes.de/tech/webdav/rfc7540.html#rfc.section.8.1.2.6.p.2 , ff) – xerx593 Jan 12 '22 at 02:48

1 Answers1

0

When attempting to send data to the backend, it is standardized that the request should be a POST request instead of a GET request. That's why axios does not let you add a body to the request.

You can use the method axios.post to do so. For example,

axios.post(url, {
  "param1": "AA",
  "param2": "BB"
}

In Spring boot, however, you're required to change the @GetMapping to a @PostMapping and add a request body to the method arguments.

@PostMapping
public String myDefaultMethod(@RequestBody Object obj) {
    // ...
}
Kypps
  • 326
  • 2
  • 5