简体   繁体   中英

An error when sending PATCH with Postman to Asp.net Core webapi

I have a model,

public class Order
{
    public Guid Id { get; set; }
    public IEnumerable<string> ItemIds { get; set; }
    public string Currency { get; set; }
}

and a repository,

public interface IOrderRepository
{
    IEnumerable<Order> Get();
    Order Get(Guid id);
    void Add(Order order);
    void Update(Guid id, Order order);
    // other irrelevant code has been deleted for simplicity
}

public class MemoryOrderRepository : IOrderRepository
{
    private readonly IList<Order> _orders = new List<Order>();
    public IEnumerable<Order> Get() => _orders;

    public Order Get(Guid id) => _orders.FirstOrDefault(o => o.Id == id);
    public void Add(Order order) => _orders.Add(order);

    public void Update(Guid id, Order order)
    {
        var target = _orders.FirstOrDefault(o => o.Id == id);
        if (target != null) target.ItemIds = order.ItemIds;
    }
    // other irrelevant code has been deleted for simplicity
}

that have been registered in Startup.cs as follows.

    public void ConfigureServices(IServiceCollection services) =>
        services.AddSingleton<IOrderRepository, MemoryOrderRepository>()
                .AddControllers()
                .AddNewtonsoftJson();

Additional relevant nuget packages are already installed as follows.

<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />

A testing controller is given as follows.

[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase
{
    private readonly IOrderRepository repo;

    public OrderController(IOrderRepository repo)
    {
        this.repo = repo;
    }

    [HttpGet]
    public IActionResult Get() => Ok(repo.Get());

    [HttpGet("{id:guid}")]
    public IActionResult Get(Guid id) => Ok(repo.Get(id));

    [HttpPost]
    public IActionResult Post(Order order)
    {
        Order target = new Order()
        {
            Id = Guid.NewGuid(),
            ItemIds = order.ItemIds
        };

        repo.Add(target);

        return CreatedAtAction(nameof(Get), new { id = target.Id }, null);
    }

    [HttpPatch("{id:guid}")]
    public IActionResult Patch(Guid id, JsonPatchDocument<Order> order)
    {
        Order target = repo.Get(id);
        if (target == null)
            return NotFound(new { Message = $"Item with id {id} does not exist." });


        order.ApplyTo(target);
        repo.Update(id, target);
        return Ok();
    }

    // other irrelevant code is deleted for simplicity
}

Using Postman, I successfully created a new order by sending POST verb to the server first and then confirming the result by sending GET verb as follows.

在此处输入图片说明

在此处输入图片说明

Question

I attempted to partially update the order with the following but failed.

在此处输入图片说明

The error message is

The JSON patch document was malformed and could not be parsed.

What is wrong and how to fix it?

Instead of

{
    "ItemIds": ["xyz","123"]
}

I also tried

{
    "op":"replace",
    "path":"/ItemIds",
    "value": ["xyz","123"]
}

Both do not help.

According to the docs for json patch you need to pass an array of operations instead of single object, like so

[
  {
    "op":"replace",
    "path":"/ItemIds",
    "value": ["xyz","123"]
  }
]

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