【发布时间】:2017-10-24 22:20:58
【问题描述】:
我对 Rest Sharp 和 Postman 还是很陌生,但我正在尝试对现有用户进行“更新”。这是我的代码,但我知道它错了。有没有人有关于如何在 Rest Sharp 中执行“替换”操作的示例?
string url = _EndPoint + "?_action = patch & _queryId =for-userName & uid =" + obj.userName.ToString();
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
string username = "openidm-admin";
string password = "openidm-admin";
string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));
string Update = BuildUpdate();
//if (Update != null)
//{
request.AddHeader("Authorization", "Basic " + svcCredentials);
request.AddHeader("content-type", "application/json");
//request.AddParameter("application/json", "[\n {\n \"operation\": \"replace\",\n \"field\": \"/userName\",\n \"value\": \"testuser4@aha.org\"\n },\n {\n \"operation\": \"replace\",\n \"field\": \"/mail\",\n \"value\": \"testuser4@aha.org\"\n }\n]", ParameterType.RequestBody);
request.AddBody({ "operation": "replace", "field": "/userName", "value": "testuser4@aha.org"}, { "operation": "replace", "field": "/mail", "value": "testuser4@aha.org"});
IRestResponse response = client.Execute(request);
邮递员说应该是这样的:
[
{
"operation": "replace",
"field": "/userName",
"value": "testuser4@aha.org"
},
{
"operation": "replace",
"field": "/mail",
"value": "testuser4@aha.org"
}
]
我更喜欢这样写,但我不知道怎么写。其他代码建议我创建一个字符串。我可以通过字符串将其写出,但如果可以或可能的话,我更愿意将其写在正文中。在此先感谢
编辑:
这是我的课:
public class IdentityDetails
{
//public const string type = "user";
//public const string realm = "dc=aha,dc=org";
public string userName { get; set; }
public string mail { get; set; }
public string givenName { get; set; }
public string sn { get; set; }
public string accountStatus { get; set; }
public string avectraId { get; set; }
public string AHApw { get; set; }
public string password { get; set; }
public string ahaBirthYear { get; set; }
public string city { get; set; }
public string ahaGender { get; set; }
public string ahaJobTitle { get; set; }
public string ahaLeadScore { get; set; }
public string stateProvince { get; set; }
public string orgId { get; set; }
public string[] ahaMemberGroup { get; set; }
public string[] ahaMemberType { get; set; }
public string regMethod { get; set; }
//public string[] ahaDrupalPermission { get; set; }
}
我认为我还需要做的是传入当前字段和值,并传入该字段的新值。我只是在寻找其他人使用 Restsharp 执行更新请求的示例代码。我可以把它全部写在一个字符串中,但我希望有一种更简单的方法,然后使用字符串并作为参数传递。
编辑
我目前正在尝试通过构建一个字符串作为参数传入来做到这一点。我知道必须有更好的方法。这是我目前在构建字符串方面的进展;
邮递员字符串:
request.AddParameter("application/javascript", "[\n {\n \"operation\": \"replace\",\n \"field\": \"/userName\",\n \"value\": \"testuser4@aha.org\"\n },\n {\n \"operation\": \"replace\",\n \"field\": \"/mail\",\n \"value\": \"testuser4@aha.org\"\n }\n]", ParameterType.RequestBody);
我的字符串生成器功能正在进行中:
private string BuildUpdate(string field, string newvalue, string oldvalue)
{
try
{
string Update = string.Empty;
string UpdateOperation = '"' + "operation" + '"' + ": " + '"' + "replace\"" + ",\n" + '"';
string Updatefield = "field" + '"' + ": \"";
string UpdateNewValue = "/" + newvalue + '"' + ",\n";
string
// "[\n {\n \"operation\": \"replace\",\n\"
// field\": \"
// /userName\",\n
// \"value\": \"testuser4@aha.org\"\n },\n {\n \"operation\": \"replace\",\n \"field\": \"/mail\",\n \"value\": \"testuser4@aha.org\"\n }\n]"
return Update;
}
catch(Exception ex)
{
return null;
}
}
谁有更好的方法来做到这一点?
【问题讨论】:
-
我相信您可以创建一个类并使用 AddBody 序列化该类的特定对象,这应该可以简化您的问题。
-
你的body应该是一个数组。
request.AddBody(new [] { .... }) -
您有可以分享的代码 sn-p 来显示您更新和替换值吗?