Sending image as base64string to WebAPI; base64 string is too long

I recently followed these tutorials by Ahsan Siddique

Developing RESTful API in ASP.Net With Azure Database.

Part 1 https://www.c-sharpcorner.com/article/creating-sql-database-in-azure-portal/

part 2 https://www.c-sharpcorner.com/article/developing-restful-api-in-asp-net-with-add-method/

Part 3 https://www.c-sharpcorner.com/article/developing-restful-apis-in-asp-net-with-retrieve-update-and-delete-functions/

Consuming RESTful API in Xamarin.Android

part 4 https://www.c-sharpcorner.com/article/consuming-restful-apis-in-xamarin-android/

I managed to get all the codes to work but I got stuck at the part where i'm trying to pass the base64 string to the web api. The tutorial didn't have the portion that I got stuck at. I tested my POST API on Postman and I get this error message, "HTTP Error 414. The request URL is too long."

Below you can see a portion of my codes:

public String BitmapToBase64(Bitmap bitmap)
{
    //Java.IO.ByteArrayOutputStream byteArrayOutputStream = new Java.IO.ByteArrayOutputStream();
    MemoryStream memStream = new MemoryStream();
    bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, memStream);
    byte[] byteArray = memStream.ToArray();
    return Base64.EncodeToString(byteArray, Base64Flags.Default);
}

User user = new User ();
user.ID = "1";
user.name = "Kelly";
user.profilepic = BitmapToBase64(NGetBitmap(uri)); //this is the part where base64string is too long

HttpClient client = new HttpClient();
string url = $"http://test.azurewebsites.net/api/User/{user.ID}?name={user.name}&profilepic={user.profilepic}";
var uri1 = new System.Uri(url); //base64
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response;
var json = JsonConvert.SerializeObject(feedback);
var content = new StringContent(json, Encoding.UTF8, "application/json");
response = await client.PostAsync(uri1, content);

if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
{
    Toast.MakeText(this, "Your profile is updated.", ToastLength.Long).Show();
}
else
{
    Toast.MakeText(this, "Your profile is not updated." + feedback.profilepic, ToastLength.Long).Show();
}

I need help! Thank you in advance!

Update: This is how my controller class currently look like

public HttpResponseMessage Update_User(int ID, string name, string profilepic)
{
    if (!ModelState.IsValid)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
    }

    UserTable newUser = new UserTable();
    var entry = db.Entry<UserTable>(newUser);
    entry.Entity.ID = ID;
    entry.Entity.name = name;
    entry.Entity.profilepic = profilepic;
    entry.State = EntityState.Modified;

    try
    {
        db.SaveChanges();
    }
    catch (DbUpdateConcurrencyException ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
    }
    return Request.CreateResponse(HttpStatusCode.Accepted, "Your profile is updated.");
}

1 answer

  • answered 2018-11-08 11:28 atoms

    As mentioned in the comments, don't send the base64 image as part of the url/GET param.

    Instead attach it to the body of the POST request.

    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("profilepic", user.profilepic)
    });
    var result = await client.PostAsync(url, content);