Added by Adam Ferguson about 10 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"?>
    <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>
    <custom_fields />

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.


Replies (7)

RE: REST API Using C# (XML schema for PUT requests) - Added by Adam Ferguson about 10 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 almost 10 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"?>
    <subject>Test Issue 2</subject>
    <description>Test Issue Number Two</description>

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 almost 10 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 almost 10 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 about 9 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 8 years ago

some useful C# xml samples