简体   繁体   English

在Angular HTTP PUT中的请求主体中传递原始数据类型

[英]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). 我有一个服务方法,我需要通过请求有效负载(即主体)使用HTTP PUT请求通过String属性将UUID传递给服务。

The signature of the Java Spring Boot API controller method is Java Spring Boot API控制器方法的签名是

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

    // Internal Operations - TODO

    return 'SUCCESS';
}

Angular HTTP PUT code: Angular HTTP PUT代码:

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: 一旦我订阅了Angular方法,我就会收到错误:

{
   "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. 我在POSTMAN中尝试过相同的功能,但是在Angular应用程序中它失败了。

You are getting that error cause the second parameter of put() expects a valid JSON. 您收到该错误导致put()的第二个参数需要有效的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 等等......这取决于您的代码中的UUID实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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