简体   繁体   中英

Angular2 how to correct put request

Ok then, I have a single problem, the

Request URL: http://.../rest/1.0/brand/test145 Request Method:PUT Status Code:500 Internal Server Error Remote Address:...

Request payload is ok { "name": "test145" }

addBrand(name : string){
    let body = JSON.stringify(name)
    let url = this.baseUrl + '/brand/' + name;
    return this.http.put(url, { name }  ).
    map(res => res.json());
}

addBrand(name:any){
if(!name){return;}
this.BrandService.addBrand(name)
.subscribe(
    name => this.name.push(name),
    error => this.errorMesage = <any>error);

}

But the put should look like http://.../rest/1.0/brand/ and then, should be only request payload. When I remove that "name" from, it doesn't load the payload.

stricly PUT is update, so the url should be in format:

http://.../rest/1.0/brand/test145 where the last part is unique id (you need to indicate what you mean to update)

If you expect to create a record, you should rather POST to http://.../rest/1.0/brand

But you can always choose to deviate from conventions. In order to PUT to .../brand with a payload { "name": "test145" } you would need to:

addBrand(name : string){
    let body = JSON.stringify({ name: name })
    let url = this.baseUrl + '/brand'
    return this.http.put(url, body ).
    map(res => res.json());
}

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