Project

General

Profile

REST API Using C#

Added by Adam Ferguson over 14 years ago

Hello. If this has already been asked, I apologize, but I could not find this information. I'm trying to use the Rest API in C#. I can get the issues for a particular problem with a standard HttpWebRequest without incident, but I cannot seem to get it to accept a PUT or POST, and the documentation is not clear enough for me.

Basically, I just build an HttpWebRequest as below:

Uri            address                = new Uri( "http://server/issues/" + issueNumber + ".xml" );
HttpWebRequest request                = HttpWebRequest.Create( address ) as HttpWebRequest;
               request.Credentials    = new NetworkCredential( "username", "password" );
               request.Method         = "PUT";
               request.ContentType    = "application/xml";
               request.Accept         = "application/xml";

Then, I build the XML which looks like this (though I have also tried it without the <issues> tag surrounding it):

<?xml version="1.0" encoding="utf-16"?>
<issues>
  <issue>
    <id>6951</id>
    <project id="28" name="Name" />
    <tracker id="3" name="Support" />
    <status id="2" name="Assigned" />
    <priority id="4" name="Normal" />
    <author id="33" name="Adam Ferguson" />
    <assigned_to id="33" name=" Adam Ferguson " />
    <subject>Test Issue 2</subject>
    <description>Test Issue Number Two</description>
    <done_ratio>20</done_ratio>
    <custom_fields />
  </issue>
</issues>

With the XML, I attempt to complete the request with the XML in "output," but I always get a 500 Server error.

byte[] bytes                    = UTF8Encoding.UTF8.GetBytes( output );
       request.ContentLength    = bytes.Length;
       request.KeepAlive        = true;

using (Stream putStream = request.GetRequestStream( ) )
{
   putStream.Write( bytes, 0, bytes.Length );
}

using( HttpWebResponse response = request.GetResponse( ) as HttpWebResponse )
{
   StreamReader reader = new StreamReader( response.GetResponseStream( ) );
   Console.WriteLine( reader.ReadToEnd( ) );
} 

I'm sure that it is something simple, but I cannot seem to figure out exactly what or how to send the information to get it to update. Any insight would be much appreciated.

Thanks,
Adam


Replies (7)

RE: REST API Using C# (XML schema for PUT requests) - Added by Adam Ferguson over 14 years ago

I've made a little bit of progress. For one thing, the XML encoding must be specified as utf-8 instead of utf-16. I have also removed the <issues> tag and made <issue> the topmost one. I can now update the fields that are their own tags (e.g. subject, description, etc), but still cannot seem to update those with id and name attributes. Is there a particular way these are supposed to be formatted? At this point, this is not really a C# issue so much as it is an issue of what sort of XML the Rest API expects. Any help would be appreciated.

RE: REST API Using C# - Added by Adam Ferguson over 14 years ago

Well, since nobody either knows or cares, I guess I'll post most of the solution that I've discovered. Why this isn't already documented (or if it is, why it is not more easily accessible), I don't know.

You can use the same C# code as in my first post; the XML is all that needs to change. Using the previous example, the correct format for the XML would be something like:

<?xml version="1.0" encoding="utf-8"?>
  <issue>
    <id>6951</id>
    <project_id>28</project_id>
    <tracker_id>3</tracker_id>
    <status_id>2</status_id>
    <priority_id>4</priority_id>
    <author_id>33</author_id>
    <assigned_to_id>33</assigned_to_id>
    <subject>Test Issue 2</subject>
    <description>Test Issue Number Two</description>
    <done_ratio>20</done_ratio>
    <custom_field_values>
      <5>98798</5>
      <6>asdf</6>
    </custom_field_values>
  </issue>

Note that the inner elements in <custom_field_values> need to be the number of the corresponding Id of your custom field. This is not correct XML, so I'm not sure why that decision was made or if there's something better and this works only by accident, but it does work. The only field I cannot determine where to place is <notes>. Looking at the production log, it would appear that the field is called simply "notes," but adding a <notes></notes> pair inside the <issue> tag doesn't seem to work. If anyone has any suggestions, I'd be happy to hear them, but it doesn't appear that anyone is interested in this. Perhaps someone else might need this information and come across this later.

RE: REST API Using C# - Added by David Horth over 14 years ago

Hi, I was wondering how far you got with these. I'm new to redmine, but I would really like to be able to automatically create issues from my application and store them in our new redmine site. I would also like to include attachments with those issues... Any thoughts

RE: REST API Using C# - Added by Adam Ferguson about 14 years ago

It actually worked pretty well using the code from my first post but using XML that looks like the 3rd. You would need to map out all of the ids mentioned in the XML fields to be sure you're updating the correct project and issue, but it does work. You can get this information by using the API pointed at issues.xml. You'd just need to GET instead of PUT. The only really strange thing I discovered is that if you want to add a comment, you don't add it via the XML, but attach it to the end of the issue's .xml address as a query string.

Something like

 http://server/issues/" + issueNumber + ".xml?notes=" + HttpUtility.UrlEncode( notes )

RE: REST API Using C# - Added by Pok Lau over 13 years ago

This looks like an API problem rather than a C# problem. I tried to change 'assigned_to' using ruby via ActiveResource and also got the funny 500 Internal Server error which is not logged either in the log our STDOUT.

RE: REST API Using C# - Added by tomi crow over 12 years ago

some useful C# xml samples

csharp.net-informations.com/xml/csharp-xmltutorial.htm

crowel.

    (1-7/7)