[英]C# RestSharp format URL
我正在使用电子签名API(GetAccept)。 我使用restsharp作为SDK,我的问题是发布到的端点看起来像这样:
https://api.getaccept.com/v1/documents/{DOCUMENT_ID}/send
在这里,我想用另一个ID(字符串是字符串)替换'DOCUMENT_ID'。
我试图将其编码为URI,但它不起作用,也尝试使用client.BuildUri();。 我尝试使用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);
}
我正在寻找一种可以正确格式化URI的解决方案,因为我认为这是问题所在。 提前致谢
这解决了我的问题。
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);
更好,更轻松的解决方案是这样的
Uri myUri = new Uri(string.format("https://api.getaccept.com/v1/documents/{0}/send",id));
根据C#的版本,您还可以使用
Uri myUsri = new Uri($"https://api.getaccept.com/v1/documents/{id}/send");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.