简体   繁体   English

C#RestSharp格式网址

[英]C# RestSharp format URL

I am using a E-signing API (GetAccept). 我正在使用电子签名API(GetAccept)。 I am using restsharp as SDK, my problem is that the endpoint I am posting to looks like this: 我使用restsharp作为SDK,我的问题是发布到的端点看起来像这样:

https://api.getaccept.com/v1/documents/{DOCUMENT_ID}/send

From here I want to replace the 'DOCUMENT_ID' with another one (id is a string). 在这里,我想用另一个ID(字符串是字符串)替换'DOCUMENT_ID'。

I have tried to encode this as a URI but it doesn't work, also I've tried to use the client.BuildUri(); 我试图将其编码为URI,但它不起作用,也尝试使用client.BuildUri();。 I have tried to use https://api.getaccept.com/v1/documents/ {0}/send and then use a string.format for that. 我尝试使用https://api.getaccept.com/v1/documents/ {0} / send,然后为此使用string.format。

public static void SendDocument()
{
    Uri myUri = new Uri("https://api.getaccept.com/v1/documents/" + id + "/send");// does not work
    Uri myUri = new Uri("https://api.getaccept.com/v1/documents/649r4tm3rz3/send");//works

    var client = new RestClient(myUri);
    var request = new RestRequest(Method.POST);
    request.AddHeader("Authorization", "Bearer " + access_token);
    IRestResponse response = client.Execute(request);
    Console.WriteLine(response.Content);
}

I'm looking for a solution that format my URI properly since I assume that is the problem. 我正在寻找一种可以正确格式化URI的解决方案,因为我认为这是问题所在。 Thanks in advance 提前致谢

This resolved my problem. 这解决了我的问题。

    string server = "https://api.getaccept.com";

                string relativePath = "/v1/documents/"+id+"/send";
                Uri serverUri = new Uri(server);
                Uri relativeUri = new Uri(relativePath, UriKind.Relative);
                Uri fullUri = new Uri(serverUri, relativeUri);


                var client = new RestClient(fullUri);
                var request = new RestRequest(Method.POST);
                request.AddHeader("Authorization", "Bearer " + access_token);
                IRestResponse response = client.Execute(request);

A better and easier solution is something like this 更好,更轻松的解决方案是这样的

Uri myUri = new Uri(string.format("https://api.getaccept.com/v1/documents/{0}/send",id));

Depending on the version of c# you could also use 根据C#的版本,您还可以使用

Uri myUsri = new Uri($"https://api.getaccept.com/v1/documents/{id}/send");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM