Rest Sharp 补丁请求
Rest Sharp Patch Request
我对 Rest Sharp 和 Postman 还是很陌生,但我正在尝试 "Update" 给现有用户。这是我的代码,但我知道它错了。有人有关于如何在 Rest Sharp 中执行 "replace" 操作的示例吗?
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"
}
]
我更愿意这样写,但我不知道怎么写。其他代码建议我制作一个字符串。我可以通过字符串写出来,但如果可以或可能的话,我更愿意将它写在正文中。提前致谢
编辑:
这是我的 class:
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;
}
}
有人有更好的方法吗?
我想我找到了问题的答案。我不确定我的顺序是否正确,但我的字符串格式正确。我希望这也能帮助其他人。
这是我使用 Restsharp 执行 REST 更新请求的基本函数:
public string UpdateRequest(string uid, string field, string newvalue, string oldvalue)
{
try
{
string url = _EndPoint + "?_action = patch & _queryId =for-userName & uid =" + uid;
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(field, newvalue, oldvalue);
if (Update != null)
{
request.AddHeader("Authorization", "Basic " + svcCredentials);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", Update, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response.Content.ToString();
}
else
{
//Error
return "Error";
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
我将我的 BuildUpdate 函数调用到 return 我将用来作为参数传入的 Json 字符串。这是我构建更新字符串的函数。我将它分成两部分并将每个部分转换为 json 字符串格式。然后我将它们组合成一个字符串:
private string BuildUpdate(string field, string newvalue, string oldvalue)
{
try
{
//Parameter String
string Update = string.Empty;
//New Value
var dict1 = new Dictionary<string, string>();
dict1.Add("operation", "replace");
dict1.Add("field", field);
dict1.Add("value", newvalue);
string json1 = Regex.Unescape(JsonConvert.SerializeObject(dict1, Newtonsoft.Json.Formatting.Indented));
//Current Value
var dict2 = new Dictionary<string, string>();
dict2.Add("operation", "replace");
dict2.Add("field", field);
dict2.Add("value", oldvalue);
string json2 = Regex.Unescape(JsonConvert.SerializeObject(dict2, Newtonsoft.Json.Formatting.Indented));
Update = json1 + ",\n " + json2;
return Update;
}
catch(Exception ex)
{
return null;
}
}
这是我将作为参数传入的输出字符串:
"{\r\n \"operation\": \"replace\",\r\n \"field\": \"userName\",\r\n \"value\": \"testuser999@aha.org\"\r\n},\n {\r\n \"operation\": \"replace\",\r\n \"field\": \"userName\",\r\n \"value\": \"testuser4@aha.org\"\r\n}"
更新:
我只是想与大家分享我的工作代码示例。
函数 1:
public string UpdateRequest(string uid, string field, string value)
{
try
{
string url = _EndPoint + "?_action=patch&_queryId=for-userName&uid=" + uid;
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(field, value);
if (Update != null)
{
request.AddHeader("Authorization", "Basic " + svcCredentials);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", Update, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response.Content.ToString();
}
else
{
//Error
return "Error";
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
这是为请求构建 Json 字符串的字符串构建函数:
private string BuildUpdate(string field, string value)
{
try
{
//Parameter String
string json = string.Empty;
//Value
var dict = new Dictionary<string, string>();
dict.Add("operation", "replace");
dict.Add("field", field);
dict.Add("value", value);
json = "[\n" + Regex.Unescape(JsonConvert.SerializeObject(dict, Newtonsoft.Json.Formatting.Indented)) + "\n]";
return json;
}
catch(Exception ex)
{
return null;
}
}
我不确定为什么我必须添加“[\n”或“\n]”,但我认为它出于某种原因将其包装起来。无论如何,我希望这对某人有所帮助。
我对 Rest Sharp 和 Postman 还是很陌生,但我正在尝试 "Update" 给现有用户。这是我的代码,但我知道它错了。有人有关于如何在 Rest Sharp 中执行 "replace" 操作的示例吗?
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"
}
]
我更愿意这样写,但我不知道怎么写。其他代码建议我制作一个字符串。我可以通过字符串写出来,但如果可以或可能的话,我更愿意将它写在正文中。提前致谢
编辑:
这是我的 class:
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;
}
}
有人有更好的方法吗?
我想我找到了问题的答案。我不确定我的顺序是否正确,但我的字符串格式正确。我希望这也能帮助其他人。
这是我使用 Restsharp 执行 REST 更新请求的基本函数:
public string UpdateRequest(string uid, string field, string newvalue, string oldvalue)
{
try
{
string url = _EndPoint + "?_action = patch & _queryId =for-userName & uid =" + uid;
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(field, newvalue, oldvalue);
if (Update != null)
{
request.AddHeader("Authorization", "Basic " + svcCredentials);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", Update, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response.Content.ToString();
}
else
{
//Error
return "Error";
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
我将我的 BuildUpdate 函数调用到 return 我将用来作为参数传入的 Json 字符串。这是我构建更新字符串的函数。我将它分成两部分并将每个部分转换为 json 字符串格式。然后我将它们组合成一个字符串:
private string BuildUpdate(string field, string newvalue, string oldvalue)
{
try
{
//Parameter String
string Update = string.Empty;
//New Value
var dict1 = new Dictionary<string, string>();
dict1.Add("operation", "replace");
dict1.Add("field", field);
dict1.Add("value", newvalue);
string json1 = Regex.Unescape(JsonConvert.SerializeObject(dict1, Newtonsoft.Json.Formatting.Indented));
//Current Value
var dict2 = new Dictionary<string, string>();
dict2.Add("operation", "replace");
dict2.Add("field", field);
dict2.Add("value", oldvalue);
string json2 = Regex.Unescape(JsonConvert.SerializeObject(dict2, Newtonsoft.Json.Formatting.Indented));
Update = json1 + ",\n " + json2;
return Update;
}
catch(Exception ex)
{
return null;
}
}
这是我将作为参数传入的输出字符串:
"{\r\n \"operation\": \"replace\",\r\n \"field\": \"userName\",\r\n \"value\": \"testuser999@aha.org\"\r\n},\n {\r\n \"operation\": \"replace\",\r\n \"field\": \"userName\",\r\n \"value\": \"testuser4@aha.org\"\r\n}"
更新: 我只是想与大家分享我的工作代码示例。
函数 1:
public string UpdateRequest(string uid, string field, string value)
{
try
{
string url = _EndPoint + "?_action=patch&_queryId=for-userName&uid=" + uid;
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(field, value);
if (Update != null)
{
request.AddHeader("Authorization", "Basic " + svcCredentials);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", Update, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response.Content.ToString();
}
else
{
//Error
return "Error";
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
这是为请求构建 Json 字符串的字符串构建函数:
private string BuildUpdate(string field, string value)
{
try
{
//Parameter String
string json = string.Empty;
//Value
var dict = new Dictionary<string, string>();
dict.Add("operation", "replace");
dict.Add("field", field);
dict.Add("value", value);
json = "[\n" + Regex.Unescape(JsonConvert.SerializeObject(dict, Newtonsoft.Json.Formatting.Indented)) + "\n]";
return json;
}
catch(Exception ex)
{
return null;
}
}
我不确定为什么我必须添加“[\n”或“\n]”,但我认为它出于某种原因将其包装起来。无论如何,我希望这对某人有所帮助。