Xamarin HTTP POST

PKPhooPKPhoo Member ✭✭✭
edited February 13 in Xamarin.Forms

Hello Developers
How can I pass filename and id to HTTP POST method?
It is my sever coding need to pass five parameters.


This is xamarin client code

How to pass parameter with HTTP POST.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Firstly, define a model class for uploading your parameters:

    public class JsonModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public Stream FileContent { get; set; }
        ... Other properties
    }
    

    Then you can initialize your HttpClient like:

    string URL = "your server url";
    HttpClient client = new HttpClient();
    
    var postBody = new JsonModel { Title = "Title ", ... };
    var content = JsonConvert.SerializeObject(postBody);
    client.PostAsync(URL, new StringContent(content));
    

    Moreover in this way, you have to install Newtonsoft.Json nuget package to serialize your model class to json string.

Sign In or Register to comment.