Home > front end >  Http Request to download file from remote server
Http Request to download file from remote server

Time:11-22

I am trying to translate the following curl command

curl --location --request GET "https://xxx.xxxx.xxx/artifacts?session=016e1f70-d9da-41bf-b93d-80e281236c46&path=/home/gauntlet_gameye/LinuxServer/Game/Saved/Logs/Game.log" -H "Authorization:Bearer xxxx" -H "Accept:application/json" --output C:\Temp\Game.log

To C# code and I have the following

string SessionId = "016e1f70-d9da-41bf-b93d-80e281236c46";
string Token = "xxxx"; 
string FilePath = "/home/gauntlet_gameye/LinuxServer/Game/Saved/Logs/Game.log";  

string Endpoint = string.Format("https://xxx.xxxx.xxx/artifacts?session={0}&path={1}", SessionId, FilePath);            
HttpRequestMessage HttpRequestMsg = new HttpRequestMessage();
HttpRequestMsg.RequestUri = new Uri(Endpoint);
HttpRequestMsg.Method = HttpMethod.Get;
HttpRequestMsg.Headers.Add("Accept", "application/json");
HttpRequestMsg.Headers.Add("Authorization", string.Format("Bearer {0}", Token));

HttpRequestMsg.Content = new StringContent(string.Format("--output {0}", OutFilePath), Encoding.UTF8, "application/json");

using (HttpClient Client = new HttpClient())
{
    var HttpResponseTask = Client.SendAsync(HttpRequestMsg);
}

But it gives me the following exception info

Cannot send a content-body with this verb-type.

CodePudding user response:

--location and --output are not C# supported options

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://xxx.xxxx.xxx/artifacts?session=016e1f70-d9da-41bf-b93d-80e281236c46&path=/home/gauntlet_gameye/LinuxServer/Game/Saved/Logs/Game.log");

request.Headers.Add("Authorization", "Bearer xxxx");
request.Headers.Add("Accept", "application/json");

HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();

Source: Convert curl commands to C#

  • Related