Unable to Load image from url in flutter - flutter

I am Fetching images from URL and it is providing broken images while on the web the same URL is providing complete and accurate images.
Scheme not starting with alphabetic character
throwing error.
// Depending on where the exception was thrown, the image cache may not
// have had a chance to track the key in the cache at all.
// Schedule a microtask to give the cache a chance to add the key.
Image Url:- https://smott.world/upload/source/1646761765-WhatsApp Image 2022-03-08 at 5.41.29 PM.jpeg

This is happening because your url is not encoded, you can encode using Uri.encodeFull to encode. Encode url will replace white spaces by %20
Example:
var uri = Uri.encodeFull('https://smott.world/upload/source/1646761765-WhatsApp Image 2022-03-08 at 5.41.29 PM.jpeg');
var url = uri.toString();
// url will be "https://smott.world/upload/source/1646761765-WhatsApp%20Image%202022-03-08%20at%205.41.29%20PM.jpeg"
You can see more about percent encoding here

Related

Flutter Image from Arabic URL

I try:
Image.network("http://ar.latifaonline.com/wp-content/uploads/2022/08/ألبوم-لطيفة-2022.png"),
but that does not work because Arabic characters convert to become :
"http://ar.latifaonline.com/wp-content/uploads/2022/08/%D8%A3%D9%84%D8%A8%D9%88%D9%85-%D9%84%D8%B7%D9%8A%D9%81%D8%A9-2022.png"
and that does not give any image but returns this error:
Exception has occurred.
HttpException (HttpException: Connection closed before full header was received, uri = http://ar.latifaonline.com/wp-content/uploads/2022/08/%D8%A3%D9%84%D8%A8%D9%88%D9%85-%D9%84%D8%B7%D9%8A%D9%81%D8%A9-2022.png)
The reason you get this error is not because of the Arabic characters, is because of http, change it to https and it will work.
if you don't have access to your backend code try this:
String yourUrl =
"http://ar.latifaonline.com/wp-content/uploads/2022/08/ألبوم-لطيفة-2022.png";
yourUrl = yourUrl.replaceFirst('http://', "https://");
Image.network(yourUrl)

How to include accented characters in Swift URL without percent encoding?

I am trying to make a web request to a URL that needs to keep accented characters instead of percent encoding them. E.g. é must NOT change to e%CC%81. I cannot change this.
These are the allowed characters that shouldn't be percent encoded: AaÁáBbCcDdEeÉéFfGgHhIiÍíJjKkLlMmNnOoÓóÖöŐőPpQqRrSsTtUuÚúÜüŰűVvWwXxYyZz0123456789-
Here is an example of a url I need
https://helyesiras.mta.hu/helyesiras/default/suggest?q=hány%20éves
You can try this url in your web borwser to confirm its working. (The site is in Hungarian.) If you try the proper percent encoded version of this url (https://helyesiras.mta.hu/helyesiras/default/suggest?q=ha%CC%81ny%20e%CC%81ves) then the website will give an error. (Also in Hungarian.)
I have my custom encoder to get this URL string. However to make a web request I need to convert the String to URL.
I tried 2 ways:
URL(string:)
let urlStr = "https://helyesiras.mta.hu/helyesiras/default/suggest?q=hány%20éves"
var url = URL(string: urlStr)
// ERROR: Returns nil
URLComponents with percentEncodedQueryItems
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "helyesiras.mta.hu"
urlComponents.path = "/helyesiras/default/suggest"
urlComponents.percentEncodedQueryItems = [ // ERROR: invalid characters in percent encoded query items
URLQueryItem(name: "q", value: "hány%20éves")
]
let url = urlComponents.url
Is it possible to create URLs without Foundation APIs checking its validity? Or can I create my own validation rules?
Safari is percent-encoding the URL. You're just percent-encoding it differently (and in a way your server is rejecting). What Safari sends to the server is:
GET /helyesiras/default/suggest?q=h%C3%A1ny%20%C3%A9ves HTTP/1.1
You can check that using Charles. Your website is behaving correctly and does not appear to require unencoded URLs.
It is not valid to send unencoded URLs, and Safari doesn't. There's no way to do it with URLSession either. You'd have to connect to the socket directly and build your own HTTP stack, which is quite possible, but I don't think you want to do that.
As Leo notes, the correct way to do this is using:
URLQueryItem(name: "q", value: "hány éves")
Replacing the %20 with the unencoded " " so that you don't double-encode the percent.
If you encode the string by hand, you'll find the same encoding:
print("hány éves".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))
// Optional("h%C3%A1ny%20%C3%A9ves")
(But you should use URLComponents. addingPercentEncoding is extremely error-prone.)
The preferred UTF-8 encoding of á is as the unicode code point LATIN SMALL LETTER A WITH ACUTE (C3 A1). What you're encoding is LATIN SMALL LETTER A (61) followed by COMBINING ACUTE ACCENT (CC 81). I suspect your server is not applying Unicode normalization rules. While that's unfortunate the fix is simple: use URLComponents, and you'll get the same correct behavior as Safari.

Is base64 encoded image uploading a bad practice?

Is there a problem to use base64 encoding to upload (only upload) the image to the server? Considering the common image size of around 1-2 MB, not icon sized images. Is this a bad practice? Should it always use form data for image uploading?
The image would be sent inside a POST body (JSON content type) together with other data, like:
// POST /signup
{
email: 'example#email.com',
password: '12345678',
name: 'Example Name',
picture: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAA...',
}
Once in the server, it would be sent to AWS bucket and get served as a binary file, not a base64 encoded string.
The generally accepted result of Base64 encoding a binary image is a result roughly 30% greater than the original. If the server limit is 2MB, you're effectively limited to a 1.4MB image as you're increasing it via encoding. Base64 isn't a compression method, it's just a method of getting binary data to a server over HTTP.
If you have control of the server, make it accept gzip compressed binary data instead, or if you can put the image somewhere, send the url of it in the request and the server can download it.
Bas64 encoded images are good practice for a small size (KB) images.
For bigger size image you will get size errors probably.
Although if you want to use (MB) size images, i suggest to pass them as a thumbnail.
Thumbnails are reduced-size versions of pictures or videos, used to help in recognizing and organizing them, serving the same role for images as a normal text index does for words
https://www.npmjs.com/package/image-thumbnail

REST style URLs where data contains /

Identifiers may sometimes contain a / character creating a URL like:
http://example.com/data/activity/EU-2010/22856/0/profile/1
where EU-2010/22856/0 is an activity identifier
so splitting the path on / results in a faulty parse
%encoding the identifier yields a URL which would parse OK
http://example.com/data/activity/EU-2010%2F22856%2F0/profile/1
but this causes Apache to throw a 404 not found error - I guess because its not a valid path now.
I could base64 encode and decode the identifier but this obfuscates the URL
Whats the recommended approach ?

Error while reading JSON url in nsstring

i am sending one url to the server
http://ogcitsco.w05.winhost.com/Service.svc/getsubmenu/south indian/restaurant/Lunch
in which i am getting bad url response, since there is one space between two words south indian, how should i work with that, i have tried to remove the space and passed the url however in response getting null value, when i paste the above url into browser it is showing the response
Remember to encode your URL first: [#"http://ogcitsco.w05.winhost.com/Service.svc/getsubmenu/south indian/restaurant/Lunch" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
a space can be changed to %20 for most languages (or + is possible too i beleve)