How add the POST data in the json request body of REST services Using C#

private void CreateNewTasklist(string TasklistTitleValue)
    {
        AuthenticationProcess objUri = new AuthenticationProcess();

        if (string.IsNullOrEmpty(TasklistTitleValue))
        {
            MessageBox.Show("Please Enter the Tasklist Title.");
        }
        else
        {
            //Read the Token result in Isolated storage.
            string TokenResult = TasklistManager.ReadTokenResult();
            var Token = Auth.ParseAuthJsonString(TokenResult);

            var request = new RestRequest(objUri.CreateTasklist, Method.POST);
            request.AddHeader("content-type", "application/json");





            RestClient client = new RestClient("https://www.googleapis.com");
            client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(Token.access_token);
            client.ExecuteAsync<TasklistManager>(request, TaskListsLoaded);
        }
    }

Here I get string TasklistTitleValue value .. i want to POST it. How to add it to request body as content-type is application/json.

Answers


You could do this:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"user\":\"test\"," +
              "\"password\":\"bla"}";

streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
}

or you can use this:

https://github.com/ademargomes/JsonRequest


Need Your Help

How to call a file in /data/data with MediaPlayer

java android media-player midi

I want Android to play a song in /data/data/com.example.myapp/ called test3.mid