1

I can use RestSharp very well sending "mono-line" things like:

"user, password, email, telephone, ... "

But I can't understand how to send multiple lines, for example, all the values in this table:

+-----------+-----------+-------+
|    Dog    |   Race    | user  |
+-----------+-----------+-------+
| Skitty    | Doberman  | User1 |
| Birillo   | Pinscher  | User2 |
| Fragolino | Corgi     | User3 |
| ...       | ...       | ...   |
+-----------+-----------+-------+

How am I supposed to format parameters for restsharp?

Thanks

Edit: as asked I show How I send "normal" data:

I take data from user input in EditText (like, string1, string2, string3), and send them to a class I use for RestSharp, and I send them with:

var client = new RestClient("x.x.x.x/app/");
var request = new RestRequest("/ServiceX", Method.POST);
request.AddParameter("Dog", string1);
request.AddParameter("Race", string2);
request.AddParameter("User", string3);
IRestResponse response = client.Execute(request);
var content = response.Content;

This is a single line in a table, I can add only 1 line per request in this way. But I want to upload more lines.

Thanks

2
  • Show how you try do it, You use POST method or what? Commented Sep 19, 2016 at 10:37
  • yes I use post, I added code on how I normally upload data, but I really don't know how to expand this on multiple lines. I didn't find anything. Thanks Commented Sep 19, 2016 at 12:03

2 Answers 2

1

Try use this. JsonHelper 1.0.2 You can download by NuGet or here

           public class YourModel
            {
                public string Doggy { get; set; }
                public string Race { get; set; }
                public string User { get; set; }

            }
            public YourModel nYModel = new YourModel();
            nYModel.Doggy = string1;
            nYModel.Race  = string2;
            nYModel.User = string3;

    var client = new RestClient(ServiceUrl);

    var request = new RestRequest("/resource/", Method.POST);

    // Json to post.
    string jsonToSend = JsonHelper.ToJson(nYModel);

    request.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody);
    request.RequestFormat = DataFormat.Json;

    try
    {
        client.ExecuteAsync(request, response =>
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                // OK
            }
            else
            {
                // NOK
            }
        });
    }
    catch (Exception error)
    {
        // Log
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot for answering! In this way I can add multiple entry for "YourModel"? or I need to make an array of "YourModel"? Thanks again!
You can make array or list.
I hope last silly question. The packet for JsonHelper? really thanks for all!
I did everything, and instead of making an array of my custom model, I did a list, working very well! Thanks for all help, i will post my code as soon as I've time.
I last used Serialize just for easy php handling, after 4 hours of echoes!! haha thanks a lot!
0

This is all the code i came up with:

In the c# app, i've a class storing the data, like

public class MyClass
    {
        public string field1{ get; set; }
        public string field2 { get; set; }
        public string field3 { get; set; }
        public string field4 { get; set; }
    }

Then I populate a List with all my entries:

List<MyClass> list= new List<MyClass>();
// make new element of MyClass like "element1"
list.Add(element1);

Well, this method is awesome because list can be populated very easily with a foreach or a while, taking all edittext in the layout, even dynamics ones, awesome.

After this next part require "Newtonsoft.Json" (NutGet is you friend... your BEST friend!)

 string json = JsonConvert.SerializeObject(list);

Well now you have to send it, I use post like I wrote in first post.

In the php part i took the data and parsed with:

$data = (json_decode($data));

This worked like a charm, becouse I can access at elements with:

foreach ($data as &$object){
  $obj->field1
  $obj->field2
  $obj->field3
  $obj->field4
}

Well now just build the query string and it's over... (just for my own desire for completeness, i paste the last part)

$query = 'INSERT INTO `table_name`(`field1`, `field2`, `field3`, `field4`) VALUES ';
foreach ($data as &$object)
    {
        $query_parts[] = "('" . $object->field1 . "', '" . $object->field2 . "', '" . $object->field3 . "', '" . $object->field4 . "')";

    }
        $query .= implode(',', $query_parts);

DONE!

Thanks @mwisnicki for the help. MANY thanks!

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.