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
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.
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();
}
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 ();
1 to 5 of 5
Comments are closed.
For more Intervals help documentation, please visit help.myintervals.com