AnsweredAssumed Answered

Update ticket using REST

Question asked by ElwynnMartin on Apr 24, 2017
Latest reply on May 4, 2017 by Maheshwar_Kusuma

I'm trying to port the java example for updating an incident to C Sharp using HTTPWebRequest and Response to make the calls and handle the returns. I'm currently getting a 400 "Bad Request" error using the code below, can someone explain what I need to change for it to work correctly?

The write doesn't seem to throw any errors, the code where it fails is the first line within the if (put.HaveResponse){.....} 

 

     private static void restPut(string accessKey)
        {
            
            
        try
        {            
         //////////////////////////////////////////////////////
        // PUT OPERATION -- Update an existing Incident ticket
        //////////////////////////////////////////////////////
        string url = "https://servername:8050/caisd-rest";
        string endpointPUT = url + "/in/400001";

        
            string postBody = @"<in>" + "<summary>Updated from REST API Java Samples code</summary>" + "</in>";
            byte[] dataByte = Encoding.ASCII.GetBytes(postBody);
        
        
        //PutMethod put = new PutMethod(endpointPUT);
       HttpWebRequest put = (HttpWebRequest)WebRequest.Create(endpointPUT);
        put.Method = "PUT";
        put.Headers.Add("X-AccessKey", accessKey);
        put.Accept = "application/json";
        put.ContentType = "application/xml; charset=UTF-8";
        //set the length of message body, it defaults to -1 meaning no data to send
        put.ContentLength = dataByte.Length;     
           
          // Get the request stream
          Stream PUTstream =  put.GetRequestStream();
                // Write the data bytes in the request stream
          PUTstream.Write(dataByte, 0, dataByte.Length);
   
        if (put.HaveResponse) {
            
            HttpWebResponse PUTResponse = (HttpWebResponse)put.GetResponse();
            StreamReader reader = new StreamReader(PUTResponse.GetResponseStream(), false);
   
            //responseString is the full responseString
            string response = reader.ReadToEnd().ToString();
            
        } else {
                    Console.WriteLine("invalid response");
        }
        
        


          //  PUTstream.Close();
        }
        catch (WebException e)
        {
            Console.WriteLine(e.ToString());
            Console.Write(e.StackTrace);
        }
        catch (IOException e)
        {
            Console.WriteLine(e.ToString());
            Console.Write(e.StackTrace);
        }
        finally
        {
            //release system resources here
        }

Outcomes