How do i reply to a ticket

I am trying to create a C# function which will allow a user to reply to a ticket but i gt a 401 Unauthorised error. Could anyopne help please. the code I have is:

public static void ReplyToTicket(long ticketId)

{

    string json = "{\"priority\" : 1,\"status\" : 2, \"body_text\" : \"ABCDEFGHI\" }";

    byte[] byteArray = Encoding.UTF8.GetBytes(json);

 

 HttpWebRequest request = HttpWebRequest)WebRequest.Create(APIPATH + ticketId.ToString() + "/reply");

    request.ContentType = "application/json";

    request.Method = Ticket.GetHttpRequestMethod(requestMethod);

    request.ContentLength = contentLength;

    string credentials = Convert.ToBase64String(Encoding.Default.GetBytes(USERNAME + ":" PASSWORD));

   

    HttpWebRequest request = GetHttpWebRequest(HttpRequestMethod.Post, json.Length, ticketId, true);

    request.ContentType = "application/json";

    Stream dataStream = request.GetRequestStream();

    dataStream.Write(byteArray, 0, byteArray.Length);

    dataStream.Close();

   

    try

    {

  WebResponse response = request.GetResponse();

        dataStream = response.GetResponseStream();

        StreamReader reader = new StreamReader(dataStream);

        string Response = reader.ReadToEnd();

    }

    catch (WebException ex)

    {

        //error handling

    }

    catch (Exception ex)

    {

     //error handling

    }

}



Hi Adam,

I see that the credentials are not sent as part of the request. Adding the line,


request.Headers["Authorization"] = "Basic " + credentials;


below the line "string credentials = ..." should work.


For further reference, please refer to the C# code samples.

Hi Soorya,


Thank you for your reply However  I am still getting the same error after adding that line.

Hi Adam,


I see that you are reconstructing the variable  request without the Authorization header. Removing the following line


    HttpWebRequest request = GetHttpWebRequest(HttpRequestMethod.Post, json.Length, ticketId, true);



and adding the Authorization header as Soorya mentioned will solve the problem.

Thank you for the reply, however I am still getting the same error.

Login or Signup to post a comment