休息夏普补丁请求

时间:2017-10-23 16:41:07

标签: c# restsharp

我对Rest Sharp和Postman仍然很新,但我正试图做一个"更新"到现有用户。这是我的代码,但我知道它错了。有没有人有关于如何执行"替换"在夏普休息时操作?

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执行更新请求的其他人的示例代码。我可以用字符串写出来,但是我希望有一种更简单的方法,然后使用字符串并作为参数传递。

修改

我目前正试图通过构建一个字符串作为参数传入来实现。我知道必须有更好的方法。以下是我目前在构建字符串方面的进展;

Postman String:

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;
            }
        } 

任何人都有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

我想我找到了问题的答案。我不确定我是否按照正确的顺序排列了这个,但我的字符串格式正确。我希望这对其他人也有帮助。

这是我使用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函数来返回我将用作传入参数的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]”但我认为它包装它是出于某种原因。无论如何,我希望这有助于某人。