在momentapp 的restful api 上的RestSharp 请求 [英] RestSharp requests on momentapp's restful api

查看:40
本文介绍了在momentapp 的restful api 上的RestSharp 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So Im trying to set up RestSharp to use Moment Task scheduling according to the docs 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

The problem is that it is always returnig HTTP 422

请帮忙.

推荐答案

这就是我的结局.在这里找到了一个样本http://johnsheehan.me/blog/building-nugetlatest-in-两小时三/

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

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

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

这篇关于在momentapp 的restful api 上的RestSharp 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆