none
Setting the If-Match in the Headers C# RRS feed

  • Question

  • I have the Odata service, in order to perform a PUT we need to set the Header with If-Match: *In Post man I giving like below and it works

    enter image description here

    How do I set this If-Match in the Odata Service call to perform a PUT operation.I tried the below

       
    private async Task<HttpResponseMessage> PutJsonAsync(string messageBody,string B_Code)
     {
     string userName = ConfigurationManager.AppSettings["Username"];
     string password = ConfigurationManager.AppSettings["Password"];
     string BaseURL = ConfigurationManager.AppSettings["BaseURL"];
    
     try
     {
       using (var httpClient = new HttpClient())
       {
           var request = new StringContent(messageBody, Encoding.UTF8, "application/json");
           string apiUrl = "SAM('" + B_Code + "')";
           request.Headers.ContentType = new MediaTypeHeaderValue("application/json");
           request.Headers.TryAddWithoutValidation("If-Match", "*");
           var url = string.Format("{0}{1}", BaseURL, apiUrl);
           var creds = userName + ":" + password;
           var credentials = Encoding.ASCII.GetBytes(creds);
           httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials));
    
           response = await httpClient.PutAsync(new Uri(url), request);
       }
      }
     catch (Exception ex)
     {
       throw ex;
     }
     return response;
      }

    But it is not working and throws error 501:Not Implemented

    `response {StatusCode: 501, ReasonPhrase: 'Not Implemented', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:

    OData-Version: 4.0 Date: Mon, 30 Oct 2017 20:42:28 GMT Set-Cookie: JSESSIONID=XYZ; Path=/; Secure; HttpOnly Set-Cookie: XYZ;PATH=/;SECURE;HTTPONLY Server: Apache-Coyote/1.1 Vary: Accept-Encoding Connection: keep-alive Content-Length: 277 Content-Type: application/json; odata.metadata=minimal }} System.Net.Http.HttpResponseMessage`


    • Edited by vdha Tuesday, October 31, 2017 5:36 AM
    • Moved by CoolDadTx Tuesday, October 31, 2017 2:11 PM ASP.NET related
    Tuesday, October 31, 2017 5:19 AM

All replies