Angular 7 http.put() 与 json 正文

Angular 7 http.put() with json body

我正在尝试使用 Angular 7 HttpClient 使用 WebAPI 资源。 HTTP PUT 操作的 CURL 成功。

curl - X PUT--header 'Content-Type: application/json'--header 'Accept: application/json' - d '%27Reviewed%27'
'https://my.organization/WebAPI/MyServices/Service1/consult/123/800088093313/reviewstatus'

我正在使用以下方法来做同样的事情,但如果我不使用 Json.stringfy() 或 Http 404,它会给我 Http 415。你能帮忙吗?谢谢。

const requestUrl = this.endpoint + '/consult/' + sta3n.toString() + '/' + consultSid.toString() + '/reviewstatus';
const requestBody = JSON.Stringfy(status);
// const requestBody = {status};
return this._http.put(requestUrl, requestBody);

这是 WebAPI 端点声明

[HttpPut]
[Route("consult/{sta3n}/{consultSID}/reviewstatus")]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(String))]
[SwaggerResponse(HttpStatusCode.Unauthorized)]
[SwaggerResponse(HttpStatusCode.NotFound)]
[SwaggerResponse(HttpStatusCode.InternalServerError, Type = typeof(Exception))]
public IHttpActionResult SetConsultReviewStatus([FromUri] short sta3n, [FromUri] long consultSID, [FromBody] string status) {
    ...
    ...
}

put 方法中,您没有提供 Content-Type header。像这样修改 http.put 行

return this._http.put(requestUrl, requestBody, , { headers: { 'Content-Type': 'application/json' } });

谢谢。