RestSharp请求momentapp的restful api

时间:2012-03-01 21:08:42

标签: c# rest restsharp

所以我试图根据文档设置RestSharp来使用Moment Task调度 http://momentapp.com/docs

这是我的代码。

public class MomentApi : ITaskScheduler
    {
        const string BaseUrl = "https://momentapp.com";    

        private RestResponse Execute(RestRequest request)
    {
        var client = new RestClient();
        client.BaseUrl = BaseUrl;
        request.AddParameter("apikey", "MYAPIKEYHERE", ParameterType.UrlSegment); // used on every request
        var response = client.Execute(request);
        return response;
    }

    public HttpStatusCode ScheduleTask(DateTime date, Uri url, string httpMethod, Uri callback = null)
    {
        var request = new RestRequest(Method.POST);
        request.Resource = "jobs.json";
        request.AddParameter("job[uri]", "http://develop.myapp.com/Something");
        request.AddParameter("job[at]", "2012-06-31T18:36:21");
        request.AddParameter("job[method]", "GET");
        var response = Execute(request);
        return response.StatusCode;
    }

问题是它总是返回HTTP 422

请帮忙。

1 个答案:

答案 0 :(得分:1)

所以这就是我最终的结果。 在这里找到了一个样本 http://johnsheehan.me/blog/building-nugetlatest-in-two-hours-3/

public HttpStatusCode ScheduleTask(DateTime date, Uri url, string httpMethod, Uri callback = null)
        {
            var request = new RestRequest("jobs.json?apikey={apikey}&job[uri]={uri}&job[at]={at}&job[method]={method}", Method.POST);
            request.AddUrlSegment("uri", "http://develop.myapp.com/Something");
            request.AddUrlSegment("at", "2012-03-31T18:36:21");
            request.AddUrlSegment("method", "GET");
            var response = Execute(request);
            return response.StatusCode;
        }

我不确定何时应该使用AddParameter以及何时应该使用AddUrlSegment 但无论如何它现在有效