Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Using Postman to test HTTPPatch in .Net Core API, [FromBody] JsonPatchDocument<TEntity> patchEntity is null

Using Postman to test HTTPPatch in .Net Core API, [FromBody] JsonPatchDocument<TEntity> patchEntity is null

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpasp-netdotnetjsonhelp
3 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thewizardoftn
    wrote on last edited by
    #1

    [HttpPatch("{id:int}")]
    public async TaskUpdatePartialAsync(int id, [FromBody] JsonPatchDocument patchEntity)
    {
    var entity = await _service.ReadAsync(id, false);
    if (entity == null)
    return NotFound();
    try
    {
    patchEntity.ApplyTo(entity, ModelState);
    }
    catch(Exception ex)
    {

                return Ok(ex.Message);
            }
            entity = await \_service.UpdateAsync(id, entity);
    
            return Ok(entity);
        }
    

    When accessing this in PostMan, the return is an error, patchEntity has one Operation with all values being null. Any ideas?

    Richard DeemingR M 2 Replies Last reply
    0
    • T thewizardoftn

      [HttpPatch("{id:int}")]
      public async TaskUpdatePartialAsync(int id, [FromBody] JsonPatchDocument patchEntity)
      {
      var entity = await _service.ReadAsync(id, false);
      if (entity == null)
      return NotFound();
      try
      {
      patchEntity.ApplyTo(entity, ModelState);
      }
      catch(Exception ex)
      {

                  return Ok(ex.Message);
              }
              entity = await \_service.UpdateAsync(id, entity);
      
              return Ok(entity);
          }
      

      When accessing this in PostMan, the return is an error, patchEntity has one Operation with all values being null. Any ideas?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      thewizardoftn wrote:

      the return is an error

      Are we supposed to guess what the error is?

      thewizardoftn wrote:

      return Ok(ex.Message);

      Returning an "OK" response for an exception seems like a particularly bad idea.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • T thewizardoftn

        [HttpPatch("{id:int}")]
        public async TaskUpdatePartialAsync(int id, [FromBody] JsonPatchDocument patchEntity)
        {
        var entity = await _service.ReadAsync(id, false);
        if (entity == null)
        return NotFound();
        try
        {
        patchEntity.ApplyTo(entity, ModelState);
        }
        catch(Exception ex)
        {

                    return Ok(ex.Message);
                }
                entity = await \_service.UpdateAsync(id, entity);
        
                return Ok(entity);
            }
        

        When accessing this in PostMan, the return is an error, patchEntity has one Operation with all values being null. Any ideas?

        M Offline
        M Offline
        Matt T Heffron
        wrote on last edited by
        #3

        You didn't check if patchEntity was null before dereferencing it.

        "Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups