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.");
}
c# xamarin.android azure-sql-database httpclient restful-authentication
add a comment |
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.");
}
c# xamarin.android azure-sql-database httpclient restful-authentication
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
add a comment |
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.");
}
c# xamarin.android azure-sql-database httpclient restful-authentication
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
c# xamarin.android azure-sql-database httpclient restful-authentication
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
add a comment |
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
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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