'Convert File in JSON Reply
I have to use JSON to query a remote site for files. The query specifies a customer number and the reply sends back files for that customer. I get the JSON reply and it includes what I think is the file inside the reply. I assume that I have to remove the JSON part of the reply. My question is how do I convert the response to a file, .PDF for example, that I can save and open later? This is all done without using a browser and most of the results from my searches seem to use one. My current query routine is:
void JSONPost(string _url, string _data, TextBox resultBox)
{
var url = _url;
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.Accept = "application/json";
httpRequest.ContentType = "application/json";
httpRequest.Headers["Authorization"] = "Bearer XXXXXXXXXXXXXXXXXXXXX";
var data = _data;
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(data);
}
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
queryResult = streamReader.ReadToEnd();
resultBox.Text = queryResult.Substring(0, 50);
}
}
Solution 1:[1]
I found that I had to perform several steps to solve: First I moved the actual file data to a new string. Then I had to remove the backslashes from the new string. I then converted the Base64 string into an array. And finally, I wrote that string to the destination file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mwryder55 |