1

I'm developing a C# application that reads local MySQL database, and send the data as POST request to online PHP server.

The PHP server is ready to receive data like this. (It is from Postman x-www-form-url-encoded)

token:da979d2922c74d7851e6f0eff2270d73
branch:JKT
items[0][code]:0001-W*XL*B
items[0][name]:018/17 DRS XL BLUE
items[0][stock]:0
items[1][code]:0001-W*XL*BK
items[1][name]:018/17 DRS XL BLACK
items[1][stock]:0

This is my c# code. I can't add array of objects to dictionary, so I'm looking for other way around.

string query = "SELECT * FROM items";
db.Connection.Open();

MySqlDataAdapter dataAdapter = new MySqlDataAdapter(query, db.Connection);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);

db.Connection.Close();

var dictionary = new Dictionary<string, string>();

dictionary.Add("token", "da979d2922c74d7851e6f0eff2270d73");
dictionary.Add("branch", "JKT");
dictionary.Add("items", dataTable); // Argument 2: cannot convert from 'System.Data.DataTable' to 'string'

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://mysite.test/sync")
{
    Content = new FormUrlEncodedContent(dictionary)
};
var response = await client.SendAsync(request);
string responseString = await response.Content.ReadAsStringAsync();

Is there other way to send POST request form data aside from Dictionary?

2
  • Is this answer any help stackoverflow.com/questions/3942427/… Commented May 28, 2019 at 12:53
  • @RiggsFolly I don't think that answer relate to my problem, I need to post array of objects meanwhile the OP there use array of string. Commented May 28, 2019 at 13:07

1 Answer 1

0

My answer may be not one that you want, but if u pass strings u can concatenate them in c# with ; separator and do explode string ; on php and get array. Also try: dictionary.Add("items[]", dataTable);

Sign up to request clarification or add additional context in comments.

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.