Xamarin parse string to URI replaces %20 with a space - string

I am trying to pass in the Uri of an Image into an UriImageSource.Uri but within the Uri String is contained %20 which gets erroneously converted into white space
i.e.
http://domain.co.uk/Category%20Name/Products/Product-image.jpg
When this gets parsed through to my function
UriImageSource UriImageSource = new UriImageSource();
UriBuilder Builder = new UriBuilder(productURL);
UriImageSource.Uri = Builder.Uri;
return UriImageSource;
the UriImageSource.Uri gets converted to
http://domain.co.uk/Category Name/Products/Product-image.jpg
This then is throwing an error in my application because the URL contains %20 rather than white space... Any help is much appreciated. Thanks!

You can encode/decode url using HttpUtility class from System.Web
More information about this class you can find here
HttpUtility doc

Related

TipTap generate JSON from a string

I am trying to generate a node from a string to set as the content for my tiptap editor.
This string could be plain text or a combination of plain text and a URL.
e.g.
const string = "Hello world https://www.example.com"
So I tried this:
import { generateJSON } from '#tiptap/core';
const output = generateJSON(string, [Document, Text, Link, Paragraph]);
However it just creates a paragraph node and doesnt link the url.
Any ideas?
TLDR: generateJSON expects a html string as the first parameter.
In Tiptap you can either work with prosemirrorJSON or html. The transformer cant know if your url should be encoded as a link if you just pass it as freetext. You should either store your data as html or prosemirrorJSON, you could use the plain text as a view use in other places or for searching. But you will need to use html or prosemirrorJSON to set the content.

Why URL module ignores characters after # in node.js

I am using url module, which basically splits a web address into readable part.
var data = url.parse(request.url).pathname
the output of request.url is C:\AppFolder\dropbox\videos\myVideo8#.MP4. After its get parsed, I dont understand why its not returing the value with "#.MP4"
I dont understand why its not returing the value with "#.MP4"
Because #.MP4 is the fragment and not the path component of the URL. (You can read up on URL syntax f.e. on WikiPedia, if you are not sure: https://en.wikipedia.org/wiki/URL#Syntax)
You want to look at hash, not pathname https://nodejs.org/docs/latest/api/url.html#url_url_hash

HTTP trigger azure function won't bind route parameter with encoded slashes

I've created an Azure C# HTTP triggered function with a route url: subscriptions/{token}/t. It works fine for urls such as subscriptions/blah/t but it fails with a 404 for parameters that contain encoded slashes: subscriptions/blah%2fblah/t. Any way around this ?
Before we get into debates, {token} is a URL encoded Base64 string which will naturally contain slashes.
This issue seems to persist.
I found out that it can be resolved by double-escaping the string, that is, applying escaping recursively two times.
token = escape(escape(token));
In .NET you can use URI.EsacpeDataString()
In JS you can use encodeURIComponent()
Note, that single escaping does not work reliably with Azure functions
but it fails with a 404 for parameters that contain encoded slashes: subscriptions/blah%2fblah/t.
It is make sense because 'subscriptions/blah%2fblah/t' is equal to 'subscriptions/blah/blah/t'. I suggest you define your own encode rule for slashes. For example, you could convert all the slashes to '[[-]]'. Your token will be like this 'subscriptions/blah[[-]]blah/t'. After received the token, you could convert the special characters back.
token = token.Replace("[[-]]", "/");

Retrofit2 request paths not working as expected

I have this Retrofit2 service definition:
#GET("/samples/{sampleId}")
Observable<Sample> getSampleById(#Path("sampleId") String sampleId);
and the base URL is http://xxx.xxx.xxx.xxx:8080/sampledb/. I doesn't work. I get 404
However, if I use as base URL this one: http://xxx.xxx.xxx.xxx:8080/ and I define the service this way:
#GET("/sampledb/samples/{sampleId}")
it works properly. Why? I don't want to put sampledb prefix in every request definition.
Retrofit 2.0 comes with new URL resolving concept. Base URL and #Url have not just simply been combined together but have been resolved the same way as what ahref in HTML does instead
Take a look at the image
Base URL: always ends with /
#Url: DO NOT start with /
For more info refer this blog
Try to remove the "/" from your GET annotation: #GET("samples/{sampleId}")

WebRequest.Create unencoding backslashes in path

I have a rest service that requires passing an encrypted key as part of the path. I urlencode the key and it works great when just placed in the browser. However, in my code I user WebRequest.Create and that appears to replace any backslashes that are generated by the encryption key. This results in the service thinking that it part of the route and fails with a 404. IS this a known defect in the .net framework or am I missing something? Seems like a pretty big deal.
Edit: (Simplified sample code)
string key = System.Web.HttpUtility.UrlEncode(TripleDESEncode("sharedkey"));
string uri = string.Format("http://mydomail.com/deposit/{0}.{1}", key, "json");
//uri looks like this here http://mydomail.com/deposit/FHnapfF5yBCEKt3%2f3YOQ5g%3d%3d.json
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
//Now the address in the HttpWebRequest is this...
//http://mydomail.com/deposit/FHnapfF5yBCEKt3/3YOQ5g%3d%3d.json
Hopefully this helps.
Ok, I ended-up making a compromise with my client and skipped the encryption for straight serializing to base64. This was only acceptable due to the nature of what I am passing. encrytion will be required in the future and I see this as a major problem that needs to be fixed. At least a workaround proposed. If I come across one I will post it.
Thanks everyone!
Final code:
HttpUtil.UrlEncode(Convert.ToBase64String(Encoding.UTF8.GetBytes("sharedKey")));
Use the UrlPathEncode method when the value is part of the path, not part of the query string:
string key = System.Web.HttpUtility.UrlPathEncode(TripleDESEncode("sharedkey"));

Resources