Recurly PUT request working but returning server error

901 views Asked by At

I am implementing the the Recurly API (http://docs.recurly.com/api) in C#/.NET and so far it has gone really well despite the lack of .NET support for V2.

I have managed to implement the majority of the API successfully but am encountering an issue with the use of PUT requests for the modification or updating of user accounts.

My GET and POST requests work perfectly and I receive httpStatusCode 200 for account creations and receive appropriate XMLs for any data I 'GET'.

However, when I attempt to, for example, reactivate a subscription using a PUT request the subscription within Recurly does get reactivated as requested, but the response I get is http status code 500:

The server encountered an error while processing your request and failed.

I believe this issue is related to the below code defining request.ContentLength = 0; but then not specifying or sending an actual body of content with the request.

Previous to defining ContentLength = 0 the subscription would not get reactivated and I would get a 411 Length Required error (hence I added the content length).

The documentation does not say anything about specifying any entity (I think that's the right term) within the PUT request, only to send a put request to the appropriate URI.

I am at a bit of an impasse and have tried incorporating a blank string, XML file (recreation of subscription details) to send with the request, but I seem to get nothing but errors returned.

I am at a loss, as I do not know what Recurly want in terms of an entity within the PUT request, and it works without one as long as ContentLength is defined which doesn't make much sense to me as I was under the impression PUT required a body as POST does, although after doing some research I found some people mentioning this may not be necessary.

I just can't get the response (httpStatusCode 200) needed for validation within the rest of my code, despite the actual request working within Recurly.

Is it possible to send a blank string or empty body of content length 0 and not get a server error or do I need to somehow find what to send to Recurly with the request for the request to return an acceptable response status code, although the latter seems redundant as the subscription gets reactivated anyway.

uri = "https://" + subdomain + ".recurly.com/v2/subscriptions/" + uuid + "/reactivate";

try
{
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
   request.Headers.Add("Authorization", "Basic " + encodeB64);
   request.Method = "PUT";
   request.ContentType = "text/XML";
   request.ContentLength = 0;
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Edit: Example of what else I have tried

uri = "https://" + subdomain + ".recurly.com/v2/subscriptions/" + uuid + "/reactivate";

try
{
    string xml = "<subscription><timeframe>now</timeframe></subscription>"; //also tried with blank string.
    byte[] arr = System.Text.Encoding.UTF8.GetBytes(xml);
    HttpWebRequest renewRequest = (HttpWebRequest)WebRequest.Create(uri);
    renewRequest.Headers.Add("Authorization", "Basic " + encodeB64);
    renewRequest.Method = "PUT";
    renewRequest.ContentType = "text/XML";
    renewRequest.ContentLength = arr.Length;

    Stream datastream = renewRequest.GetRequestStream();
    datastream.Write(arr, 0, arr.Length);
    datastream.Close();

    HttpWebResponse renewResponse = (HttpWebResponse)renewRequest.GetResponse();

}

As a side note, I am new to C# and therefore have limited knowledge on it whilst I am learning so bear with me!

Thanks

1

There are 1 answers

0
Ben Maxfield On BEST ANSWER

For anyone else who gets stuck on this due to lack of knowledge of the API or documentation, it would appear that some of my headers were wrong/missing.

Here is the code to satisfy the Recurly servers.

try
{
    HttpWebRequest renewRequest = (HttpWebRequest)WebRequest.Create(uri);
    renewRequest.Headers.Add("Authorization", "Basic " + encodeB64);
    renewRequest.Method = "PUT";
    renewRequest.ContentLength = 0;
    renewRequest.UserAgent = "mylib/1.0";
    renewRequest.Host = "XXXX.recurly.com";
    renewRequest.Accept = "application/xml";

    HttpWebResponse renewResponse = (HttpWebResponse)renewRequest.GetResponse();
}    

As you can see the Accept header is now application/xml, and the userAgent header was missing, this seems to have resolved the internal server error message.