Blank Pdf file getting downloaded

Last Reply 14 days ago By dharmendr

Posted 17 days ago

Hi

I am using NimbusPost manifest APi. It is getting blank file downloaded

        public void Manifest(string awb, string token)
        {
            var client = new RestClient("htps://api.nimbuspost.com/v1/shipments/manifest");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");

            string awbNo = awb;
            string val1 = "NMBC0001789312";
            var body = $@"{{
                ""awbs"": [
                    ""{awbNo}"",
                    ""{val1}""
                ]
            }}";

            request.AddParameter("application/json", body, ParameterType.RequestBody);
            request.AddHeader("Authorization", "Bearer " + token);
            IRestResponse response = client.Execute(request);

            dynamic json = JsonConvert.DeserializeObject(response.Content);
            string pdf = json.data;
            
            string s = pdf;
            int idx = s.LastIndexOf('/');
            string fileName = s.Substring(idx + 1);
            using (var client5 = new System.Net.WebClient())
            {
                byte[] bytes5 = client5.DownloadData(pdf);
                File.WriteAllBytes(Server.MapPath("~/App_Data/") + fileName, bytes5);
            }

Thanks

You are viewing reply posted by: dharmendr 14 days ago.
Posted 14 days ago

Refer this code.

C#

Root root = JsonConvert.DeserializeObject<Root>(response.Content);
using (WebClient webClient = new WebClient())
{
    byte[] bytes = webClient.DownloadData(root.data);
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(root.data));
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}

Class

public class Root
{
    public bool status { get; set; }
    public string data { get; set; }
}