cancel
Showing results for 
Search instead for 
Did you mean: 

cannot download a file "downloadable"

cannot download a file "downloadable"

I've got a product downloadable and I want to download it using the link "obfuscated" in "my downloadable products". The link works in the browser, and works in Postman using the same token and phpsessid cookie of the browser. The problem is when I try to download the file using the code in c#. The response that I receive with the RestClient does not contain the file. After many attempts I discovered that the problem is related only to the phpsessid cookie: the one obtained asking for the token with a rest client is not valid for the download.

example of the code in c#:

             void takeTheToken()
            {
                var client = new RestClient("http://mymagentowebsite/index.php/rest/V1/integration/customer/token");
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                string userName = "myusername@mydomain.com";
                string password = "mypassword";
                string credentials = Convert.ToBase64String( Encoding.ASCII.GetBytes(userName + ":" + password));
                string authorization = string.Format("Basic {0}", credentials);
                request.AddHeader("Authorization", authorization);
                request.AddHeader("Content-Type", "application/json");
                request.AddParameter("application/json", "{\"username\":\"myusername@mydomain.com\", \"password\":\"mypassword\"}", ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                RestResponseCookie primoCookie = response.Cookies[0] as RestResponseCookie;
                string phpSessId = primoCookie.Value;

                string bearerToken = (response.Content).Replace("\"", "");
                FileDownload(bearerToken,phpSessId);
            }

            void FileDownload(string token ,string phpSessId)
            {
                string uriString = "https://mymagentowebsite/downloadable/download/link/id/MC41MDk0NjcwMCAxNjA2MDU4NDQ5MjQyMDQ4/";
                WebClient myWebClient = new WebClient();              
                myWebClient.Headers.Add("Authorization", "Bearer " + token);
                myWebClient.Headers.Add("Cookie", "PHPSESSID="+phpSessId);
                myWebClient.Headers.Add("Host", "mymagentowebsite");
                myWebClient.Headers.Add("Accept-Encoding", "gzip, deflate, br");
                myWebClient.DownloadFile(uriString, @"e:\temp\myfile.myextension");
            }

Is there a way to get a good PHPSESSID ?