Skip to content

Bug: MultipartFormDataContent not works on web assembly #62

Closed

Description

@Ali-YousefiTelori

this code works on uwp project but not work on web assembly project
Test example:

    public static class SignalGoHelper
    {
        public static string HostUrl { get; set; } = "http://88.99.244.66:3817/";
        private static HttpClient CreateHttp()
        {
#if __WASM__
		    var handler = new Uno.UI.Wasm.WasmHttpHandler();
            var httpClient = new HttpClient(handler);			
#else
            var httpClient = new HttpClient();

#endif

            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));
            return httpClient;
        }

        public static async Task<T> PostDataAsync<T>(string url, params Tuple<string, object>[] parameters) where T : class, new()
        {
            var taskCompletionSource = new TaskCompletionSource<T>();
            url = HostUrl + url;
            MultipartFormDataContent form = new MultipartFormDataContent();
            var request = CreateHttp();
            foreach (var item in parameters)
            {
                Console.WriteLine(item.Item1 + " " + item.Item2);
                var jsonPart = new StringContent(JsonConvert.SerializeObject(item.Item2), Encoding.UTF8, "application/json");
                jsonPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
                jsonPart.Headers.ContentDisposition.Name = item.Item1;
                jsonPart.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                form.Add(jsonPart);
            }
            var response = await request.PostAsync(url, form);
            var responsePayload = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<T>(responsePayload);
        }
    }
        private async void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var result = await SignalGoHelper.PostDataAsync<MessageContract<UserInfo>>("Authentication/Login",
                    new Tuple<string, object>("userName", txtUsername.Text), new Tuple<string, object>("password", txtPassword.Text));
                if (result.IsSuccess)
                {
                    
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
            }
        }

Activity

  1. changed the title [-]Bug: MultipartFormDataContent not worl on web assembly[/-] [+]Bug: MultipartFormDataContent not works on web assembly[/+] on Jun 23, 2018
  2. Ali-YousefiTelori commented on Jun 24, 2018

    @Ali-YousefiTelori
    Author

    I test again that works, no bug was there but my mistake

  3. jeromelaban commented on Jun 26, 2018

    @jeromelaban
    Member

    Thank you for the clarification, glad it's working for you.

  4. added
    question
    platform/wasm 🌐
    on Jun 26, 2018
  5. added a commit that references this issue on Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    platform/wasm 🌐question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions