Not signed in (Sign In)
The Intervals Forum is read-only
Please head to help.myintervals.com for help articles and guides. If you have any questions, please contact our support team.

API

C# Post time

Bottom of Page

1 to 5 of 5

  1.  
  1.  

    I'm trying to log time against a current task, but get the error: "The remote server returned an error: (400) Bad Request."

    I'm an amateur c# developer and have come up following code:


    HttpWebRequest request = WebRequest.Create("https://api.myintervals.com/time/?taskid=1028237") as HttpWebRequest;
    request.Method = "POST";
    request.Accept = "application/xml";
    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("XXXXXX:")));
    StreamWriter writer = new StreamWriter(request.GetRequestStream());
    writer.Write(@"<?xml version=""1.0"" encoding=""UTF-8""?>
    <time>
    <worktypeid>164205</worktypeid>
    <personid>56602</personid>
    <date>2011-08-02</date>
    <time>07:46:00</time>
    <billable>f</billable>
    </time>");
    writer.Flush();
    // Get response
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
    // Get the response stream
    StreamReader reader = new StreamReader(response.GetResponseStream());

    // Console application output
    textBox1.Text = reader.ReadToEnd();
    }


    Any ideas? Or samples I can look at?

    Thanks,
    R

    • jprado
    • Aug 2nd 2011 edited @ 08/02/2011 11:45 am
     

    Take out ?taskid=1028237 from the request URL and add it in the xml instead:

    <taskid>1028237</taskid>

    Also make sure that worktypeid is the correct id for the worktype. This id can be retrieved by listing the worktypes for the project.

    • Riaan
    • Aug 3rd 2011 edited @ 08/04/2011 12:00 am
     

    Thank you very much for the response. I tried your suggestion, but did not work. I made some changes to the xml, but I think the problem is with the way I set up the request?

    Should I use any encryption or format the xml in any specific way?
    Would you please send a C# sample of how the to do a "POST"?

    --my code--
    HttpWebRequest request = WebRequest.Create("https://api.myintervals.com/time/") as HttpWebRequest;
    request.Method = "POST";
    request.Accept = "application/xml";
    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("XXXXXXX:")));

    StreamWriter writer = new StreamWriter(request.GetRequestStream());
    writer.Write(@"<?xml version=""1.0"" encoding=""UTF-8""?>
    <time>
    <taskid>1028237</taskid>
    <worktypeid>164205</worktypeid>
    <personid>56602</personid>
    <date>2011-08-02</date>
    <time>1</time>
    <billable>f</billable>
    </time>");
    writer.Flush();

    // Get response
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {

    // Get the response stream
    StreamReader reader = new StreamReader(response.GetResponseStream());

    // Console application output

    textBox1.Text = reader.ReadToEnd();
    }

  2.  

    Here is a C# sample:

    // Create a request using a URL that can receive a post.
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.myintervals.com/time/");
    request.Accept = "application/xml";
    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("xx:")));

    // Set the Method property of the request to POST.
    request.Method = "POST";
    // Create POST data and convert it to a byte array.
    string postData = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    <time>
    <taskid>1028237</taskid>
    <worktypeid>164205</worktypeid>
    <personid>56602</personid>
    <date>2011-08-02</date>
    <time>10</time>
    <billable>t</billable>
    </time>";
    byte[] byteArray = Encoding.UTF8.GetBytes (postData);
    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/xml";
    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;
    // Get the request stream.
    Stream dataStream = request.GetRequestStream ();
    // Write the data to the request stream.
    dataStream.Write (byteArray, 0, byteArray.Length);
    // Close the Stream object.
    dataStream.Close ();
    // Get the response.
    WebResponse response = request.GetResponse ();
    // Display the status.
    Console.WriteLine (((HttpWebResponse)response).StatusDescription);
    // Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream ();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader (dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd ();
    // Display the content.
    Console.WriteLine (responseFromServer);
    // Clean up the streams.
    reader.Close ();
    dataStream.Close ();
    response.Close ();

  3.  
    Thank you very much, this works perfectly!

Comments are closed.
For more Intervals help documentation, please visit help.myintervals.com