【问题标题】:RestSharp query returning NotFoundRestSharp 查询返回 NotFound
【发布时间】:2021-08-09 18:33:57
【问题描述】:

这是我第一次使用 RestSharp

我正在尝试使用他们的 FormsAPI (https://legacydocs.hubspot.com/docs/methods/forms/submit_form) 连接到 HubSpot

使用 .Net、C#、MVC。

当我在 Fiddler 中运行时,它可以工作。

这是我的 C# 代码,当我运行它时,我得到一个“未找到”的状态代码。我确定这很简单吗?

 var client = new RestClient("https://api.hsforms.com");
        var request = new RestRequest("submissions/v3/integration/submit/{PortalId}/{formGuid}", Method.POST);


    request.AddUrlSegment("portalId", "[myportalid]");
    request.AddUrlSegment("formGuid", "[myformid]");
    request.AddQueryParameter("hapikey", "[myapikey]");
    request.RequestFormat = DataFormat.Json;
    request.AddParameter("firstname", "testfirstname");
    request.AddParameter("lastname", "testlastname");
    request.AddParameter("email", "testemail@emailaddress.com");
    request.AddParameter("business_unit", "Test");

【问题讨论】:

    标签: json asp.net-mvc restsharp


    【解决方案1】:

    最好创建c#类的模型,序列化为Json,发送POST请求。

    1. RestSharp 请求示例
            public async Task SendHubSpotRequest()
            {
                var PortalId = 1;
                var formGuid = 1;
    
                var client = new RestClient("https://api.hsforms.com");
                var request = new RestRequest($"submissions/v3/integration/submit/{PortalId}/{formGuid}", Method.POST);
    
                var hubSpotRequest = new HubSpotRequest()
                {
                    SubmittedAt = "1517927174000",
                    Fields = new Field[]
                    {
                        new Field() { Name = "email", Value = "testemail@emailaddress.com" },
                        new Field() { Name = "firstname", Value = "testfirstname" },
                        new Field() { Name = "lastname", Value = "testlastname" }
                    },
                    Context = new Context
                    {
                        Hutk = "hutk",
                        PageUri = "www.example.com/page",
                        PageName = "Example page"
                    },
                    LegalConsentOptions = new LegalConsentOptions
                    {
                        Consent = new Consent
                        {
                            // Fill other params
                        }
                    }
                };
                request.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(hubSpotRequest), ParameterType.RequestBody);
    
                var response = await client.ExecuteAsync(request);
                var responseContent = response.Content;
            }
    
    1. body json 模型的 C# 类
    public class HubSpotRequest
            {
                [JsonProperty("submittedAt")]
                public string SubmittedAt { get; set; }
    
                [JsonProperty("fields")]
                public Field[] Fields { get; set; }
    
                [JsonProperty("context")]
                public Context Context { get; set; }
    
                [JsonProperty("legalConsentOptions")]
                public LegalConsentOptions LegalConsentOptions { get; set; }
            }
    
            public class Context
            {
                [JsonProperty("hutk")]
                public string Hutk { get; set; }
    
                [JsonProperty("pageUri")]
                public string PageUri { get; set; }
    
                [JsonProperty("pageName")]
                public string PageName { get; set; }
            }
    
            public class Field
            {
                [JsonProperty("name")]
                public string Name { get; set; }
    
                [JsonProperty("value")]
                public string Value { get; set; }
            }
    
            public class LegalConsentOptions
            {
                [JsonProperty("consent")]
                public Consent Consent { get; set; }
            }
    
            public class Consent
            {
                [JsonProperty("consentToProcess")]
                public bool ConsentToProcess { get; set; }
    
                [JsonProperty("text")]
                public string Text { get; set; }
    
                [JsonProperty("communications")]
                public Communication[] Communications { get; set; }
            }
    
            public class Communication
            {
                [JsonProperty("value")]
                public bool Value { get; set; }
    
                [JsonProperty("subscriptionTypeId")]
                public long SubscriptionTypeId { get; set; }
    
                [JsonProperty("text")]
                public string Text { get; set; }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-23
      • 2012-05-29
      • 2022-11-16
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多