himak
himak

Reputation: 144

Convert cURL to WebRequest Equivalent in C#, cURL having result dowload jon file as zip output '-o response.zip'

I'm trying to convert cURL to C# web-request equivalent. I'm able to convert cURL to C# without response download from cURL (-o response.zip). But only problem I have is, I have no idea in c# code to translate/convert the cURL code for (-o response.zip) response.zip download file cURL syntax.

Below is my cURL.

curl -X GET -H "Content-Type: application/json" -H "X-API-TOKEN: 123456789asdfghj1234qqwerewrty" "https://yourdatacenterid.test.com/API/v3/responseexports/ES_1234sdfasas13wer/file" -o response.zip

Below is the code so far I have,

 string baseURLWithResponseID = "https://yourdatacenterid.test.com/API/v3/responseexports/ES_1234sdfasas13wer/file";

    var request = (HttpWebRequest)WebRequest.Create(new Uri(baseURLWithResponseID));
    request.Method = "GET";
    request.AllowAutoRedirect = false;
    request.ContentType = "application/json";
    request.Accept = "*/*";
    request.Headers.Add("X-API-TOKEN", "123456789asdfghj1234qqwerewrty");
    // Here I'm missing how to convert -o response.zip as C# code ?

    var response = request.GetResponse();

     using (var streamReader = new StreamReader(stream: response.GetResponseStream()))
                {
                var jsonresult = streamReader.ReadToEnd();

                Response.Write(jsonresult.ToString()); //I'm able to print the response json as a string but I want it to be downloaded as a zip file

                }

I got below as result from the string print which is not in a format. enter image description here

Please help me to get the API response as json downloadable file in C# from cURL syntax. Thank you.

Update 1:

I have modified code as below.

    string baseURLWithResponseID = "https://yourdatacenterid.test.com/API/v3/responseexports/ES_1234sdfasas13wer/file";

string path = "c:\\API_Test\\response.json";
var request2 = (HttpWebRequest)WebRequest.Create(new Uri(baseURLWithResponseID));
 request2.Method = "GET";
request2.AllowAutoRedirect = false;
 request2.ContentType = "application/json"; // Tried with "application/gzip" but output no change
 request2.Accept = "*/*";
 request2.Headers.Add("X-API-TOKEN", "123456789asdfghj1234qqwerewrty");
  request2.Headers.Add("Accept-Encoding", "gzip,deflate"); // I added this new line 
   var responseFile = request2.GetResponse();

  using (var src = responseFile.GetResponseStream())
          {
            using (var dst = File.Create(path))
             {
                    src.CopyTo(dst);
               }

           }

Here is the output Json file, I'm not sure still in Json file is also getting non readable format. if I try same from post man application I got it as a zip file and if I unzip that I could able to read a clean json file.

enter image description here

Here is the image from API trial version from third party website. Please check my cURL conversion to c#, Am I missing anything ?

enter image description here

Upvotes: 0

Views: 2160

Answers (2)

Halis S.
Halis S.

Reputation: 446

You can use built-in AutomaticDecompression property of HttpWebRequest. Just add the following line after creating request:

request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

Upvotes: 0

Gusman
Gusman

Reputation: 15161

That's not JSON, it's the zip file's content, so you just need to read the stream binarily and write the data to a file.

.net also offers a very easy way to do this, Stream.CopyTo, just copy the response stream to a file stream and you're done:

    string baseURLWithResponseID = "https://yourdatacenterid.test.com/API/v3/responseexports/ES_1234sdfasas13wer/file";

    var request = (HttpWebRequest)WebRequest.Create(new Uri(baseURLWithResponseID));
    request.Method = "GET";
    request.AllowAutoRedirect = false;
    request.ContentType = "application/json";
    request.Accept = "*/*";
    request.Headers.Add("X-API-TOKEN", "123456789asdfghj1234qqwerewrty");

    var response = request.GetResponse();

    using (var src = response.GetResponseStream())
    {
        using (var dst = File.Create("Path to wherever you want to store the file"))
        {
                src.CopyTo(dst);
        }

    }

Upvotes: 1

Related Questions