none
Patch Request With Content Data RRS feed

  • Question

  • I am facing bad request in response when trying to fire SendAsync request.

    Can anyone share any example of how to send PATCH request with data content in it.

     
    Tuesday, November 7, 2017 1:49 PM

All replies

  • PATCH is a rarely supported verb. In order for it to work the remote server has to support a PATCH request (most do not). If it does then a PATCH request is generally identical to a POST request but you only send the data that has changed. Again though, the remote server must explicitly support the PATCH verb. Refer to the documentation for the API you're calling to see if they allow PATCH. If they do then they will often indicate the minimal information you have to send for the request.

    Based upon your error I'd say they don't. But only the API devs can help with the bad data request.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, November 7, 2017 2:36 PM
    Moderator
  • Hello Shubhangi27,

    There is a way that uses HttpWebrequest to send Patch request with data content and I tested it for web Api. The following is my simple demo.

     string data = @"{
                    'Email': 'xxxx@example.com',
                    'Active': true}
                    ";
                string url = "http://localhost:57310/api/values";
                Encoding encoding = Encoding.Default;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "PATCH";
                request.ContentType = "application/json; charset=utf-8";
                byte[] buffer = encoding.GetBytes(data);
               Stream dataStream = request.GetRequestStream();
                dataStream.Write(buffer, 0, buffer.Length);
                dataStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                string result = "";
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default))
                {
                    result= reader.ReadToEnd();
                }
                MessageBox.Show(result);

    And my web service code

     public class User
        {
            public string Email { get; set; }
            public bool Active { get; set; }
        }
        public class ValuesController : ApiController
        {
            [HttpPatch]
            public IEnumerable<string> Patch([FromBody]User user)
            {
                ...
                ...
                return new string[] { "OK", user.Email };
            }
    }

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 8, 2017 10:22 AM
    Moderator
  • Hello ,

    Is there anything updated? If the issues has been solved, you could close the thread by marking the useful reply as answer, This can be beneficial to other community members reading this thread.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 15, 2017 10:44 AM
    Moderator
  • Hello,

    Thanks, I followed this and it worked.  

    Thursday, November 30, 2017 4:59 PM