locked
How to send MultipartFormDataContent data in body to API RRS feed

  • Question

  • User392521 posted

    In my App, I want to send MultipartFormDataContent to API call. In MultipartFormDataContent contains json data , strings & image file. I know to send json data to API call but i am facing issue with image.

    Thanks, Jagdeesh.

    Tuesday, May 12, 2020 3:47 PM

Answers

  • User371688 posted

    You can refer to the following code:

    string url = "https://myurl.com/api/...";
    string filename = "myFile.png";
    // In my case this is the JSON that will be returned from the post
    string result = "";
    // 1. Create a MultipartPostMethod
    // "NKdKd9Yk" is the boundary parameter
    
    using (var formContent = new MultipartFormDataContent("NKdKd9Yk"))
    {
        formContent.Headers.ContentType.MediaType = "multipart/form-data";
        // 3. Add the filename C:\\... + fileName is the path your file
        Stream fileStream = System.IO.File.OpenRead("C:\\Users\\username\\Pictures\\" + fileName);
        formContent.Add(new StreamContent(fileStream), fileName, fileName);
    
        using (var client = new HttpClient())
        {
            // Bearer Token header if needed
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _bearerToken);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
    
            try
            {
                // 4.. Execute the MultipartPostMethod
                var message = await client.PostAsync(url, formContent);
                // 5.a Receive the response
                result = await message.Content.ReadAsStringAsync();                
            }
            catch (Exception ex)
            {
                // Do what you want if it fails.
                throw ex;
            }
        }    
    }
    

    // 5.b Process the reponse Get a usable object from the JSON that is returned

      MyObject myObject = JsonConvert.DeserializeObject<MyObject>(result);
    

    Refer : https://stackoverflow.com/a/50398388/10308336

    And there is some similar threads about this question, you can check them here: https://stackoverflow.com/questions/19954287/how-to-upload-file-to-server-with-http-post-multipart-form-data/29118333

    https://www.codeproject.com/Questions/5223384/Upload-image-using-postasync

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, May 13, 2020 7:23 AM

All replies

  • User371688 posted

    You can refer to the following code:

    string url = "https://myurl.com/api/...";
    string filename = "myFile.png";
    // In my case this is the JSON that will be returned from the post
    string result = "";
    // 1. Create a MultipartPostMethod
    // "NKdKd9Yk" is the boundary parameter
    
    using (var formContent = new MultipartFormDataContent("NKdKd9Yk"))
    {
        formContent.Headers.ContentType.MediaType = "multipart/form-data";
        // 3. Add the filename C:\\... + fileName is the path your file
        Stream fileStream = System.IO.File.OpenRead("C:\\Users\\username\\Pictures\\" + fileName);
        formContent.Add(new StreamContent(fileStream), fileName, fileName);
    
        using (var client = new HttpClient())
        {
            // Bearer Token header if needed
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _bearerToken);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
    
            try
            {
                // 4.. Execute the MultipartPostMethod
                var message = await client.PostAsync(url, formContent);
                // 5.a Receive the response
                result = await message.Content.ReadAsStringAsync();                
            }
            catch (Exception ex)
            {
                // Do what you want if it fails.
                throw ex;
            }
        }    
    }
    

    // 5.b Process the reponse Get a usable object from the JSON that is returned

      MyObject myObject = JsonConvert.DeserializeObject<MyObject>(result);
    

    Refer : https://stackoverflow.com/a/50398388/10308336

    And there is some similar threads about this question, you can check them here: https://stackoverflow.com/questions/19954287/how-to-upload-file-to-server-with-http-post-multipart-form-data/29118333

    https://www.codeproject.com/Questions/5223384/Upload-image-using-postasync

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, May 13, 2020 7:23 AM
  • User392521 posted

    Thank you @jezh

    Wednesday, May 13, 2020 8:41 PM