2017-10-23 11 views
0

私はRest SharpとPostmanにはまだ新しいですが、私は既存のユーザーに "Update"をしようとしています。ここに私のコードがありますが、私は間違いを知っています。 Rest Sharpで "replace"操作を実行する方法のサンプルを誰かが持っていますか?Rest Sharp Patch Request

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\": \"[email protected]\"\n },\n {\n \"operation\": \"replace\",\n \"field\": \"/mail\",\n \"value\": \"[email protected]\"\n }\n]", ParameterType.RequestBody); 
      request.AddBody({ "operation": "replace", "field": "/userName", "value": "[email protected]"}, { "operation": "replace", "field": "/mail", "value": "[email protected]"}); 
      IRestResponse response = client.Execute(request); 

ポストマンは、それがこのようなものであるべきと言う:

[ 
    { 
    "operation": "replace", 
    "field": "/userName", 
    "value": "[email protected]" 
    }, 
    { 
    "operation": "replace", 
    "field": "/mail", 
    "value": "[email protected]" 
    } 
] 

私はそのように書くことを好むだろうが、私はどのようには考えています。他のコードは、私は文字列を作ることを提案した。私はストリングを介してそれを書くことができますが、私は可能であれば、身体に書き込むことを好む。事前

編集のおかげで:ここで

は私のクラスである:

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\": \"[email protected]\"\n },\n {\n \"operation\": \"replace\",\n \"field\": \"/mail\",\n \"value\": \"[email protected]\"\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\": \"[email protected]\"\n },\n {\n \"operation\": \"replace\",\n \"field\": \"/mail\",\n \"value\": \"[email protected]\"\n }\n]" 

       return Update; 
      } 
      catch(Exception ex) 
      { 
       return null; 
      } 
     } 

誰もがこれを行うには良い方法がありますか?

+0

私はクラスを作成し、そのクラスの特定のオブジェクトを直列化するためにAddBodyを使用することができます、あなたの問題を簡素化する必要がありますと信じています。 –

+0

あなたの体は配列でなければなりません。 'request.AddBody(new [] {....})' – Amy

+0

共有して値を更新して置き換えることを示すコードスニペットがありますか? – mholmes3038

答えて

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

私は私が私のパラメータとして渡すために使用する私のJSON文字列を返すために、私のBuildUpdate関数を呼び出しています。ここで私の更新文字列を作成する関数です。私は2つの部分に分割し、各部分を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\": \"[email protected]\"\r\n},\n {\r\n \"operation\": \"replace\",\r\n \"field\": \"userName\",\r\n \"value\": \"[email protected]\"\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] "しかし、私は何らかの理由でそれをラップすると仮定します。とにかく、これが誰かを助けることを望む。

+0

私の答えはちょっと間違っているかもしれないと思います。私はdict1変数だけが必要かもしれません。私が与えられたドキュメントには、混乱するサンプルがあります。ですから、他の誰かのために出力文字列から+ "、\ n" + json2を削除すれば、それはあなたのためにはうまくいくはずです。行は次のようになります。Update = json1; – mholmes3038

関連する問題