Hi
It is giving error NimbusAPi.shipping.root has some Invalid arguments. In response below data is returned
{"status":true,"data":"htts:\/\/nimubs-assets.s3.amazonaws.com\/manifest\/20230116150815-95.pdf"}
C#
dynamic json = JsonConvert.DeserializeObject(response.Content);
JavaScriptSerializer serializer = new JavaScriptSerializer();
Root root = serializer.Deserialize(json);
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();
}
Sachin SinghRank #66 · 26.2K reputationPosted Jan 16, 2023, 4:54 AM EST
Is your root class like this?
public class Root { public bool status { get; set; } public string data { get; set; } }Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 16, 2023, 4:55 AM EST
Use Newtonsoft.Json and try below code.
public class Root { [JsonPropertyName("status")] public bool Status { get; set; } [JsonPropertyName("data")] public string Data { get; set; } } var response = "{\"status\":true,\"data\":\"htts:\\/\\/nimubsassets.s3.amazonaws.com\\/manifest\\/20230116150815-95.pdf\"}"; var responseContent = JsonConvert.DeserializeObject(response);Ramco RamcoRank #462 · 5.4K reputationPosted Jan 16, 2023, 4:59 AM EST
Hi Sachin
I want to get the pdf file downloaded from the data returned by Api
Thanks
Sachin SinghRank #66 · 26.2K reputationPosted Jan 16, 2023, 5:06 AM EST
make htts to https in your url (root.data). there is no scheme like htts.
Ramco RamcoRank #462 · 5.4K reputationPosted Jan 16, 2023, 5:09 AM EST
Hi Sachin
that is not the issue . I have done that because otherwise i am able to raise issue on forum
Thanks
Sachin SinghRank #66 · 26.2K reputationPosted Jan 16, 2023, 5:35 AM EST
then if it is deserializing correctly then it should download?
is it deserializing correctly? JsonConvert.Deserialize not giving any error?
Pasang TamangRank #449 · 5.6K reputationPosted Jan 16, 2023, 8:54 AM EST
Hi,
What I notice in your response example is either you made typo while copying or you are getting wrong data. In your data I see htts. That should be https. You should receive response like this {"status":true,"data":"https:\/\/nimubs-assets.s3.amazonaws.com\/manifest\/20230116150815-95.pdf"}. And with Newtonsoft.Json you can do as in below code example.
public class Root { [JsonPropertyName("status")] public bool Status { get; set; } [JsonPropertyName("data")] public string Data { get; set; } }var root = JsonConvert.DeserializeObject(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(); }Regards,
Pasang