0

What is the Correct format for sending JSON using RestSharp:

Example PUT JSON:
 {
 "properties": [
{
  "name": "description",
  "value": "A far better description than before"
}
]
}

In C# how to correctly send, I'm attempting with:

     request.AddJsonBody(new
        {
            properties = new[]
            {
                new{property="name",value="about_us"},
                new{property="value",value="My description"}
            }
        });

Below is the full code:

  private void UpdateCompanyProperty(string companyId)
    {

        var hapikey = "{YOUR_HAPI_KEY_HERE}";
        var client = new RestClient("https://api.hubapi.com/");
        var request = new RestRequest("companies/v2/companies/{companyId}", Method.PUT);
        request.AddUrlSegment("companyId", companyId);
        request.AddQueryParameter("hapikey", hapikey);
        request.RequestFormat = DataFormat.Json;

        request.AddJsonBody(new
        {
            properties = new[]
            {
                new{property="name",value="about_us"},
                new{property="value",value="My description"}
            }
        });

        IRestResponse response = client.Execute(request);

        JObject jObject = JObject.Parse(response.Content);
        JToken jvid = jObject["portalId"];

        Debug.WriteLine(jvid);

    }

No errors but not updating or returning values.

2 Answers 2

0

Try my answer here:

https://stackoverflow.com/a/57281157/5478655

request.RequestFormat = DataFormat.Json; // Important

var input = new Dictionary<string, object>();
// props could be an array or real objects too of course
var props = new[]
{
    new{property="name",value="about_us"},
    new{property="value",value="My description"}
};
input.Add("properties", props);

request.AddBody(input);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I also added a response if anyone else is looking.
0

Create a class and give it any name

class MyClass
{
   public string property {get;set;}
   private string value {get;set;}
}

Define your class as an object

List<MyClass> list = new List<MyClass>
{
   new MyClass() { property = "name", value = "about_us"},
   new MyClass() { property = "value", value = "My Description"},
};

Now using Newtonsoft.Json serialize your object

string result = JsonConvert.SerializeObject(list);

Now add it to an array

 var resArray = new object[] { result };

Find your modified code below

class MyClass
    {
       public string property {get;set;}
       private string value {get;set;}
    }

using Newtonsoft.Json;
using RestSharp;

private void UpdateCompanyProperty(string companyId)
{


List<MyClass> list = new List<MyClass>
    {
       new MyClass() { property = "name", value = "about_us"},
       new MyClass() { property = "value", value = "My Description"},
    };

string result = JsonConvert.SerializeObject(list);

    var hapikey = "{YOUR_HAPI_KEY_HERE}";
    var client = new RestClient("https://api.hubapi.com/");
    var request = new RestRequest("companies/v2/companies/{companyId}", Method.PUT);
    request.AddUrlSegment("companyId", companyId);
    request.AddQueryParameter("hapikey", hapikey);
    request.RequestFormat = DataFormat.Json;

    request.AddJsonBody(new
    {
        properties =result
    });

    IRestResponse response = client.Execute(request);

    JObject jObject = JObject.Parse(response.Content);
    JToken jvid = jObject["portalId"];

    Debug.WriteLine(jvid);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.