Recurly PUT请求工作,但返回服务器错误错误、服务器、工作、Recurly

2023-09-04 05:51:02 作者:怀中猫

我实现了Recurly API( http://docs.recurly.com/api )在C#/。NET,到目前为止它已经真的很好,尽管缺乏V2 .NET的支持。

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.

我已经成功地实现了广大API成功,但我遇到一个问题,使用PUT请求对用户帐户的修改或更新。

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.

我的GET和POST请求的工作完美,我收到则httpStatus code 200帐户的创作,并得到适当XMLS任何数据I'GET'。

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

然而,当我尝试,例如,使用一个PUT请求中Recurly订阅激活订阅的确实的得到重新的要求,但我得到的响应是HTTP状态code 500

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.

我相信这个问题是关系到低于code定义 request.ContentLength = 0; 但不指定​​或发送的内容的实际身体的要求。

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界定 CONTENTLENGTH = 0 订阅将不会被重新激活,我会得到一个411长度必需的错误(因此我添加的内容长度)。

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).

该文件并没有说明PUT请求中指定的任何实体(我认为这是正确的术语)任何东西,只是送的在的PUT请求到相应的URI。

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.

我在一个有点僵局,并尝试引入一个空字符串,XML文件(订阅信息娱乐)发送该请求,但我似乎什么也没有返回的错误。

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.

我很茫然,因为我不知道是什么Recurly要在PUT请求中的实体而言,和它的作品无一只要 CONTENTLENGTH 终止定义这没有多大意义,我为我是根据需要作为POST对身体的IM pression PUT,虽然做了一些研究之后,我发现有些人提这可能不是必要的。

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.

我就不能得到所需要的我的code,其余内确认的响应(则httpStatus code 200),尽管实际的请求中Recurly工作。

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.

是否可以发送一个空字符串或空主体内容长度为0,而不是得到了一个服务器错误还是我需要以某种方式找到发送给Recurly与请求的请求返回可接受的响应状态code,尽管后者似乎是多余的作为预订得到反正激活。

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();

编辑:还有什么我都试过示例

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();

}

作为一个方面说明,我是新来的C#和它因此具有有限的知识,而我学习使大家多多包涵!

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

感谢

推荐答案

对于任何人谁得到缺少的API或文档知识停留在此因,它会出现一些我的头都是错误/丢失。

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.

下面是code,以满足Recurly服务器。

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();
}    

正如你所看到的Accept头现在的应用程序/ XML和的userAgent头不见了,这似乎已经解决了内部服务器错误消息。

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.