How to send file with HttpClient post in xamarin forms

amr_kamalamr_kamal ✭✭✭Member ✭✭✭

I want to send file through post request using httpclient this what i tried but file didn't sent , when i tried in postman it works fine

string Url = $"http://ataprojects.net/test/products.php?request_type=add&company_name={BaseService.Company}&name={product.name}&barcode={product.barcode}&buy_price={product.buy_price}&sell_price={product.sell_price}";
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var content = new MultipartFormDataContent();
                    content.Headers.ContentType.MediaType = "multipart/form-data";
                    content.Add(new StreamContent(product._mediaFile.GetStream()),
                      "image",
                       product.image);                    
                    var response = client.PostAsync(Url, content).Result;

                    response.EnsureSuccessStatusCode();
                    if (response.IsSuccessStatusCode)
                    {
                        var contentdata = await response.Content.ReadAsStringAsync();                       
                        var Items = JsonConvert.DeserializeObject<AddProductReturnModel>(contentdata);
                        return Items;
                    }
                    else
                    {
                        return null;
                    }
                }

            }

what's the problem ?

Best Answer

  • amr_kamalamr_kamal ✭✭✭ ✭✭✭
    Accepted Answer

    I Solved it by changing the key like in postman "image"

Answers

  • RylaRyla ✭✭ Member ✭✭

    try this
    ` public async static Task CheckDevice()
    {

            string res = "";
            if (CrossConnectivity.Current.IsConnected == true){
                try
                {
                HttpClient client = new HttpClient();
                client.BaseAddress = "YOUR APİ;
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("aplication/json"));
                client.DefaultRequestHeaders.Add("DeviceCode",DeviceId);
                client.DefaultRequestHeaders.Add("Secret", Config.Secret);
                HttpResponseMessage response = await client.GetAsync("YOUR APİ);
                if (response.IsSuccessStatusCode)
                    {
                    res =  response.Content.ReadAsStringAsync().Result;
    
                }
                    else
                    {
                    //  res = "Err:" + await response.Content.ReadAsStringAsync();
                    res = null;
                    }
                }
                catch (Exception ex)
                {
                    res = "Err:" + ex.Message;
                }
            }
            else
            {
    
                await Application.Current.MainPage.DisplayAlert("Warning!", "no Internet connection", "ok");
                if (DeviceInfo.Platform == Device.Android)
                {
                    Thread.CurrentThread.Abort();
                }
                else if (DeviceInfo.Platform == Device.iOS)
                {
                    Thread.CurrentThread.Abort();
                }
            }
            return res;
        }`
    
  • amr_kamalamr_kamal ✭✭✭ Member ✭✭✭

    @Ryla It's post request with file , sample like what i did in postman
    i followed this link

    https://github.com/HoussemDellai/UploadFileToServer

    but doesn't work for me in mobile side

  • amr_kamalamr_kamal ✭✭✭ Member ✭✭✭
    Accepted Answer

    I Solved it by changing the key like in postman "image"

Sign In or Register to comment.