Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slashes in a query string parameter?

How can I send a file path as a query string parameter?

This is my string parameter:

//domain/documents/Pdf/1234.pdf

I have tried that:

    [HttpPost]
    [Route("documents/print/{filePath*}")]
    public string PrintDocuments([FromBody] string[] docs,string filePath)
    {
       .....
    }

But this is not working, I guess because of the double slashes in the beginning of the parameter.

Any idea?

like image 288
user3378165 Avatar asked May 09 '16 15:05

user3378165


People also ask

Should there be a slash before query string?

Show activity on this post. No. It is not correct to skip the slash. It may work modern browsers: however, that does not make it correct.

What is %20 in query string?

According to the W3C (and they are the official source on these things), a space character in the query string (and in the query string only) may be encoded as either " %20 " or " + ".

What characters are allowed in query string?

Letters ( A – Z and a – z ), numbers ( 0 – 9 ) and the characters ' ~ ',' - ',' . ' and ' _ ' are left as-is. + is encoded by %2B. All other characters are encoded as a %HH hexadecimal representation with any non-ASCII characters first encoded as UTF-8 (or other specified encoding)

What is %27 in query string?

The %27 is ASCII for the single quote ( ' ) and that is a red flag for someone trying to perform SQL injection via the query string to your application's data access layer logic.


1 Answers

If, like you say, that entire string is the parameter, and not a route, you will need to URL encode it. You should always do this anyway:

System.Net.WebUtility.UrlEncode(<your string>);
// %2F%2Fdomain%2Fdocuments%2FPdf%2F1234.pdf

Update

Since that is not working, I would suggest you Base64 encode it instead of URL encode it:

var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(<your string>);
var encodedFilePath = System.Convert.ToBase64String(plainTextBytes);

..and in your controller decode it:

byte[] data = Convert.FromBase64String(filepath);
string decodedString = Encoding.UTF8.GetString(data);
like image 144
Crowcoder Avatar answered Oct 09 '22 08:10

Crowcoder