Search code examples
restspring-mvcspring-bootcurlrestful-architecture

The document has moved Calling a Rest API method


I have a basic SpringBoot app. using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I've defined this Rest method to update a User

@PutMapping(path = "/api/users/{id}", 
                        consumes = "application/json", 
                        produces = "application/json")
    public ResponseEntity<User> updateUser
                                    (HttpServletRequest request, 
                                    @PathVariable long id,
                                    User user) {

        System.out.println(user);
        saveUser (user)
        return ResponseEntity.ok(user);

    }

I call this method from the console, using

curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIazUxMiJ9.eyJzdWIiOiJyaWNhcmQub2xsZUBnbWFpbC5jb20iLCJleHAiOjE1MjgxMTM3NTIsImlhdCI6MTUyNzUwODk1Mn0.QdxabtU1U87pYvyTstT1EG3E6uVpLo2mXCF0FF8iD6acKoAXKl_A0-eV_GrpOFg5FF1qR6B7llI5_USJL85YTQ" -d {"id":1,"username":"pere.peris@gmail.com","email":"pere.peris@gmail.com","firstName”:”Pere”,”lastName”:”PERIS”,”country”:”CAT”,”enabled":true} "http://127.0.0.1:2233/elcor/api/users/1"

but what I see in the console is really strange

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/gmail/">here</A>.
</BODY></HTML>
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/gmail/">here</A>.
</BODY></HTML>
curl: (3) Port number ended with 'R'
curl: (3) Port number ended with 'P'
curl: (3) Port number ended with 'E'
curl: (3) Port number ended with 'i'
curl: (3) Port number ended with 'a'
curl: (3) Port number ended with 'm'
curl: (3) Port number ended with 'm'
curl: (3) Port number ended with 'c'
curl: (3) Port number ended with 't'
curl: (3) Port number ended with 't'
curl: (3) Port number ended with 't'
curl: (3) Port number ended with 't'
curl: (3) [globbing] bad range specification in column 14
curl: (3) Port number ended with 't'
{"id":1,...."enabled":false}

Solution

  • You have to escape the body content that you provided to the -d argument and surround it in quotes like this:

    -d "{\"id\":1,\"username\":\"pere.peris@gmail.com\",\"email\":\"pere.peris@gmail.com\",\"firstName\":\"Pere\",\"lastName\":\"PERIS\",\"country\":\"CAT\",\"enabled\":true}"
    

    Otherwise everything outside of quotes will be treated as a command by the shell.