RestSharp requests on momentapp's restful api

2019-07-13 11:23发布

So Im trying to set up RestSharp to use Moment Task scheduling according to the docs http://momentapp.com/docs

here is my code.

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

The problem is that it is always returnig HTTP 422

please help.

1条回答
狗以群分
2楼-- · 2019-07-13 12:09

So this is what I ended up with. found a sample here 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;
        }

Im not completely sure on when I should use AddParameter and when I should use AddUrlSegment but anyways it works now

查看更多
登录 后发表回答