Update Mingle Card Data

Follow

Comments

5 comments

  • Avatar
    Mark Richter

    We have created a .NET assembly that wraps the Mingle API. We'll post the binaries and release the source very soon. In the meantime I'm happy to handle Q&A here and give you code snippets as examples.


    We're not using WCF to wrap Mingle's RESTful API. Here's a snippet of code from where we set HttpWebRequest to Mingle and handle an HttpWebResponse in return.


     



            private HttpWebResponse Response(HttpWebRequest request)


            {
                HttpWebResponse response;
                var unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(_password);

                try
                {
                    ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertifications;

                    // Manually setting the Authorization request header avoids an unnecessary 401 round trip when .NET
                    // tries an unauthenticated request by default first. We are also keeping the password using SecureString.
     
                    var authBytes =
                        Encoding.UTF8.GetBytes(
                            string.Format("{0}:{1}", _login, Marshal.PtrToStringUni(unmanagedString)).ToCharArray());

                    request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);

                    response = (HttpWebResponse) request.GetResponse();
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("401 Unauthorized"))
                        throw new MingleUnauthorizedException();
                    else
                        throw;
                }
                finally
                {
                    Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
                }
                return response;
            }


     


            private static bool AcceptAllCertifications(object sender, X509Certificate certification, X509Chain chain,
                                                        SslPolicyErrors sslPolicyErrors)
            {
                return true;
            }


  • Avatar
    ArsinEffecta

    Thanks for a quick reply.


    Presently, we are able to do GET (fetch Cards data) and POST ( Create new card) operations by using REST API and C#.Net.


    But unable to perform PUT request i.e . Update Mingle Card data through REST API and C#.Net .


    Could you please help on this sort?

  • Avatar
    Mark Richter

    The key to PUT/POST is to properly package the post data payload and set the correct ContentType. Here's the code we use to package a request. The IEnumerable passed into the Request method enumerates the already-formatted members of the payload whether they be for GET, PUT or POST.



            private static HttpWebRequest Request(string requestType, string url, IEnumerable<string> data)
            {
                HttpWebRequest request;
                var finalUrl = new StringBuilder(url);

                // Encode the request data
                switch (requestType)
                {
                    case "PUT":
                    case "POST":
                        {
                            var postData = new StringBuilder();

                            request = (HttpWebRequest)WebRequest.Create(url);
                            request.Method = requestType;
                            request.ContentType = "application/x-www-form-urlencoded";

                            // Add parameter from data if any
                            if (data != null) data.ToList().ForEach(s => postData.Append(s + "&"));

                            var encodedBytes = Encoding.UTF8.GetBytes(postData.Remove(postData.Length - 1, 1).ToString());

                            // Write the encoded data to the request.
                            using (var stream = request.GetRequestStream())
                            {
                                stream.Write(encodedBytes, 0, encodedBytes.Length);
                            }

                            break;
                        }

                    case "GET":
                        {
                            finalUrl = new StringBuilder(url + "?");

                            if (data != null) data.ToList().ForEach(s => finalUrl.Append(s + "&"));

                            finalUrl.Remove(finalUrl.Length - 1, 1);

                            request = (HttpWebRequest)WebRequest.Create(finalUrl.ToString());
                            request.Method = requestType;
                            request.ContentType = "text/html";

                            break;
                        }

                    default:
                        request = (HttpWebRequest)WebRequest.Create(url);
                        break;
                }

                // Finalize the request
                var uri = new Uri(finalUrl.ToString());
                EnablePreAuthentication(uri, "basic");
                return request;
            }


  • Avatar
    ArsinEffecta

    We are calling the above method like this.


    List<string> myList = new List<string>();
    myList.Add("card[name]=A new story card");
    IEnumerable<string> myEnumerable = myList;

    HttpWebRequest req = Request("POST", strURL, myEnumerable);


    But it does n't work for both PUT and POST.


    Is there anything wrong in List object or List items as Card name and Card Description?

  • Avatar
    Mark Richter

    @ArsinEffects, it appears you may be experiencing a nuance in the use of PUT and POST with Mingle's API. When updating cards you use PUT. When creating cards you use POST. They are not interchangeable.

Please sign in to leave a comment.