简体   繁体   中英

Pass a primitive data type in request body in the Angular HTTP PUT

I have a service method and I need to pass a UUID through a String property to the service using an HTTP PUT request through request payload (ie, body).

The signature of the Java Spring Boot API controller method is

@PutMapping("group/{groupId}/updateFile")
    public String deleteAgencyFile(@PathVariable("groupId") UUID reportId,@RequestBody UUID fileId) {

    // Internal Operations - TODO

    return 'SUCCESS';
}

Angular HTTP PUT code:

updateGroupFile() {
    const groupId: string = 'b9ddab47-56a2-11e9-8882-484d7ee28bcd';
    const fileId: string = 'c0822353-56a2-11e9-8882-484d7ee28bcd';

    const filePath = `http://localhost:3000/api/group/${groupId}/updateFile`

    return this.http.put<SimpleResponse>(filePath, fileId);
}

Once I'm subscribing to the Angular method I'm getting the error:

{
   "timestamp":"2019-04-04T06:30:32.098+0000",
   "status":400,
   "error":"Bad Request",
   "message":"JSON parse error: Unrecognized token 'c0822353': was expecting 'null', 'true', 'false' or NaN; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'c0822353': was expecting 'null', 'true', 'false' or NaN\n at [Source: (PushbackInputStream); line: 1, column: 10]",
   "path":"/api/group/b9ddab47-56a2-11e9-8882-484d7ee28bcd/updateFile"
}

I tried the same in POSTMAN, it's working but in the Angular application it fails.

You are getting that error cause the second parameter of put() expects a valid JSON. Valid code would be

updateGroupFile() {
    const groupId: string = 'b9ddab47-56a2-11e9-8882-484d7ee28bcd';
    const fileId: string = 'c0822353-56a2-11e9-8882-484d7ee28bcd';

    const filePath = `http://localhost:3000/api/group/${groupId}/updateFile`

    return this.http.put<SimpleResponse>(filePath, {fileId});
}

or if you want a specific param name then

return this.http.put<SimpleResponse>(filePath, {'id': fileId});
return this.http.put<SimpleResponse>(filePath, {'uuid': fileId});

and so on ... It depends on your UUID implementation in your code as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM