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











up vote
-2
down vote

favorite












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.");
}









share|improve this question




















  • 1




    Why are you including the image in the URL rather than in the body of the POST?
    – mjwills
    Nov 8 at 11:22






  • 2




    The base64 encoded image should not be part of the query string/URL. Please add it to the body using a post request
    – phuzi
    Nov 8 at 11:22










  • As already replied by other users: you should use the POST method here. Not only to overcome the problem that the data is too big to fit in a URI, but also to use the HTTP protocol as intended: GET is for read actions, POST (and other methods like PUT and DELETE) are for update actions. You may want to have a look at w3schools.com/tags/ref_httpmethods.asp
    – johey
    Nov 8 at 13:51

















up vote
-2
down vote

favorite












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.");
}









share|improve this question




















  • 1




    Why are you including the image in the URL rather than in the body of the POST?
    – mjwills
    Nov 8 at 11:22






  • 2




    The base64 encoded image should not be part of the query string/URL. Please add it to the body using a post request
    – phuzi
    Nov 8 at 11:22










  • As already replied by other users: you should use the POST method here. Not only to overcome the problem that the data is too big to fit in a URI, but also to use the HTTP protocol as intended: GET is for read actions, POST (and other methods like PUT and DELETE) are for update actions. You may want to have a look at w3schools.com/tags/ref_httpmethods.asp
    – johey
    Nov 8 at 13:51















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











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.");
}









share|improve this question















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.");
}






c# xamarin.android azure-sql-database httpclient restful-authentication






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 13:45









Rob

1,038920




1,038920










asked Nov 8 at 11:19









worrier

11




11








  • 1




    Why are you including the image in the URL rather than in the body of the POST?
    – mjwills
    Nov 8 at 11:22






  • 2




    The base64 encoded image should not be part of the query string/URL. Please add it to the body using a post request
    – phuzi
    Nov 8 at 11:22










  • As already replied by other users: you should use the POST method here. Not only to overcome the problem that the data is too big to fit in a URI, but also to use the HTTP protocol as intended: GET is for read actions, POST (and other methods like PUT and DELETE) are for update actions. You may want to have a look at w3schools.com/tags/ref_httpmethods.asp
    – johey
    Nov 8 at 13:51
















  • 1




    Why are you including the image in the URL rather than in the body of the POST?
    – mjwills
    Nov 8 at 11:22






  • 2




    The base64 encoded image should not be part of the query string/URL. Please add it to the body using a post request
    – phuzi
    Nov 8 at 11:22










  • As already replied by other users: you should use the POST method here. Not only to overcome the problem that the data is too big to fit in a URI, but also to use the HTTP protocol as intended: GET is for read actions, POST (and other methods like PUT and DELETE) are for update actions. You may want to have a look at w3schools.com/tags/ref_httpmethods.asp
    – johey
    Nov 8 at 13:51










1




1




Why are you including the image in the URL rather than in the body of the POST?
– mjwills
Nov 8 at 11:22




Why are you including the image in the URL rather than in the body of the POST?
– mjwills
Nov 8 at 11:22




2




2




The base64 encoded image should not be part of the query string/URL. Please add it to the body using a post request
– phuzi
Nov 8 at 11:22




The base64 encoded image should not be part of the query string/URL. Please add it to the body using a post request
– phuzi
Nov 8 at 11:22












As already replied by other users: you should use the POST method here. Not only to overcome the problem that the data is too big to fit in a URI, but also to use the HTTP protocol as intended: GET is for read actions, POST (and other methods like PUT and DELETE) are for update actions. You may want to have a look at w3schools.com/tags/ref_httpmethods.asp
– johey
Nov 8 at 13:51






As already replied by other users: you should use the POST method here. Not only to overcome the problem that the data is too big to fit in a URI, but also to use the HTTP protocol as intended: GET is for read actions, POST (and other methods like PUT and DELETE) are for update actions. You may want to have a look at w3schools.com/tags/ref_httpmethods.asp
– johey
Nov 8 at 13:51














1 Answer
1






active

oldest

votes

















up vote
0
down vote













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);





share|improve this answer





















  • Hi! Thanks for your fast reply but as I am still a newbie. I am wondering how my controller class should look like after I have added your codes. I have included the codes above.
    – worrier
    Nov 8 at 12:10










  • Sorry, I don't understand what you are after. Does the suggested change work? If this is a separate question to the original, please mark as accepted and open a new question relating to the issue.
    – atoms
    Nov 8 at 12:14












  • I am trying to store base64string in Azure SQL database. I am currently developing a Xamarin Android app.
    – worrier
    Nov 8 at 12:24






  • 1




    then please open a new question related to the issue you are experiencing. If this has solved the issue outlined in the original post, please mark as accepted
    – atoms
    Nov 8 at 12:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206712%2fsending-image-as-base64string-to-webapi-base64-string-is-too-long%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













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);





share|improve this answer





















  • Hi! Thanks for your fast reply but as I am still a newbie. I am wondering how my controller class should look like after I have added your codes. I have included the codes above.
    – worrier
    Nov 8 at 12:10










  • Sorry, I don't understand what you are after. Does the suggested change work? If this is a separate question to the original, please mark as accepted and open a new question relating to the issue.
    – atoms
    Nov 8 at 12:14












  • I am trying to store base64string in Azure SQL database. I am currently developing a Xamarin Android app.
    – worrier
    Nov 8 at 12:24






  • 1




    then please open a new question related to the issue you are experiencing. If this has solved the issue outlined in the original post, please mark as accepted
    – atoms
    Nov 8 at 12:25















up vote
0
down vote













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);





share|improve this answer





















  • Hi! Thanks for your fast reply but as I am still a newbie. I am wondering how my controller class should look like after I have added your codes. I have included the codes above.
    – worrier
    Nov 8 at 12:10










  • Sorry, I don't understand what you are after. Does the suggested change work? If this is a separate question to the original, please mark as accepted and open a new question relating to the issue.
    – atoms
    Nov 8 at 12:14












  • I am trying to store base64string in Azure SQL database. I am currently developing a Xamarin Android app.
    – worrier
    Nov 8 at 12:24






  • 1




    then please open a new question related to the issue you are experiencing. If this has solved the issue outlined in the original post, please mark as accepted
    – atoms
    Nov 8 at 12:25













up vote
0
down vote










up vote
0
down vote









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);





share|improve this answer












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);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 11:28









atoms

1,71911020




1,71911020












  • Hi! Thanks for your fast reply but as I am still a newbie. I am wondering how my controller class should look like after I have added your codes. I have included the codes above.
    – worrier
    Nov 8 at 12:10










  • Sorry, I don't understand what you are after. Does the suggested change work? If this is a separate question to the original, please mark as accepted and open a new question relating to the issue.
    – atoms
    Nov 8 at 12:14












  • I am trying to store base64string in Azure SQL database. I am currently developing a Xamarin Android app.
    – worrier
    Nov 8 at 12:24






  • 1




    then please open a new question related to the issue you are experiencing. If this has solved the issue outlined in the original post, please mark as accepted
    – atoms
    Nov 8 at 12:25


















  • Hi! Thanks for your fast reply but as I am still a newbie. I am wondering how my controller class should look like after I have added your codes. I have included the codes above.
    – worrier
    Nov 8 at 12:10










  • Sorry, I don't understand what you are after. Does the suggested change work? If this is a separate question to the original, please mark as accepted and open a new question relating to the issue.
    – atoms
    Nov 8 at 12:14












  • I am trying to store base64string in Azure SQL database. I am currently developing a Xamarin Android app.
    – worrier
    Nov 8 at 12:24






  • 1




    then please open a new question related to the issue you are experiencing. If this has solved the issue outlined in the original post, please mark as accepted
    – atoms
    Nov 8 at 12:25
















Hi! Thanks for your fast reply but as I am still a newbie. I am wondering how my controller class should look like after I have added your codes. I have included the codes above.
– worrier
Nov 8 at 12:10




Hi! Thanks for your fast reply but as I am still a newbie. I am wondering how my controller class should look like after I have added your codes. I have included the codes above.
– worrier
Nov 8 at 12:10












Sorry, I don't understand what you are after. Does the suggested change work? If this is a separate question to the original, please mark as accepted and open a new question relating to the issue.
– atoms
Nov 8 at 12:14






Sorry, I don't understand what you are after. Does the suggested change work? If this is a separate question to the original, please mark as accepted and open a new question relating to the issue.
– atoms
Nov 8 at 12:14














I am trying to store base64string in Azure SQL database. I am currently developing a Xamarin Android app.
– worrier
Nov 8 at 12:24




I am trying to store base64string in Azure SQL database. I am currently developing a Xamarin Android app.
– worrier
Nov 8 at 12:24




1




1




then please open a new question related to the issue you are experiencing. If this has solved the issue outlined in the original post, please mark as accepted
– atoms
Nov 8 at 12:25




then please open a new question related to the issue you are experiencing. If this has solved the issue outlined in the original post, please mark as accepted
– atoms
Nov 8 at 12:25


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206712%2fsending-image-as-base64string-to-webapi-base64-string-is-too-long%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)