我試圖按如下方式執行與JsonServiceClient補丁到服務棧API:內容類型必須是「應用/ JSON-補丁+ JSON」 JsonServiceClient ServiceStack
var patchRequest = new JsonPatchRequest
{
new JsonPatchElement
{
op = "replace",
path = "/firstName",
value = "Test"
}
};
_jsonClient.Patch<object>($"/testurl/{id}", patchRequest);
但我發現了出現以下錯誤:
Content-Type must be 'application/json-patch+json'
錯誤很明顯。有沒有辦法在執行JsonServiceClient的請求之前更改內容類型?
這是ServiceStack API請求POCO:
[Api("Partial update .")]
[Route("/testurl/{Id}」, "PATCH")]
public class PartialTest : IReturn<PartialTestRequestResponse>, IJsonPatchDocumentRequest,
IRequiresRequestStream
{
[ApiMember(Name = 「Id」, ParameterType = "path", DataType = "string", IsRequired = true)]
public string Id { get; set; }
public Stream RequestStream { get; set; }
}
public class PartialTestRequestResponse : IHasResponseStatus
{
public ResponseStatus ResponseStatus { get; set; }
}
服務實現:
public object Patch(PartialTest request)
{
var dbTestRecord = Repo.GetDbTestRecord(request.Id);
if (dbTestRecord == null) throw HttpError.NotFound("Record not found.");
var patch =
(JsonPatchDocument<TestRecordPoco>)
JsonConvert.DeserializeObject(Request.GetRawBody(), typeof(JsonPatchDocument<TestRecordPoco>));
if (patch == null)
throw new HttpError(HttpStatusCode.BadRequest, "Body is not a valid JSON Patch Document.");
patch.ApplyTo(dbTestRecord);
Repo.UpdateDbTestRecord(dbTestRecord);
return new PartialTestResponse();
}
我使用Marvin.JsonPatch V 1.0.0庫。
這不是ServiceStack中的錯誤,請更新您的帖子以包含ServiceStack服務實現。 – mythz