adryr
adryr

Reputation: 140

Passing a url encoded byte array

I need to pass a byte array via a URL. So I am encoding it with the UrlEncode Method like this:

string ergebnis = HttpUtility.UrlEncode(array);

The result is the following string: %00%00%00%00%00%25%b8j

Now when I pass this string in a URL like this http://localhost:51980/api/Insects?Version=%00%00%00%00%00%25%b8j

This is my Get function:

        public List<TaxonDiagnosis> Get([FromUri] string version)
        {
            List<TaxonDiagnosis> result = new List<TaxonDiagnosis>();

            result = db.TaxonDiagnosis.ToList();

            byte[] array = HttpUtility.UrlDecodeToBytes(version);

            if (version != null)
                result = db.GetTaxonDiagnosis(array).ToList();

            return result;
        }

The problem is, version's value isn't %00%00%00%00%00%25%b8j. Instead it is this \0\0\0\0\0%�j. This of course causes problems when I try to decode it into a byte array again.

How can I pass the correct string in the Url?

Upvotes: 1

Views: 9611

Answers (1)

adryr
adryr

Reputation: 140

As suggested by Jon Skeet, I encoded the arraywith a URL-safe base64 decodabet like in this post: How to achieve Base64 URL safe encoding in C#?

Upvotes: 2

Related Questions