Consuming compressed JSON using HttpClient and Jil - c#

I'm a little confused about how to properly deserialize gziped Json payload from a HttpClient instance.
So far I'm doing the following, but it seems wrong. At least too complicated. Can't I feed a stream to Jil? Can't the HttpClient unzip the stream?
var client = new HttpClient();
var userEndPoint = new Uri(baseUri, "api/login");
var request = new HttpRequestMessage();
request.RequestUri = userEndPoint;
request.Method = HttpMethod.Get;
var response = _client.SendAsync(request).Result;
var userGzipByteArray = response.Content.ReadAsByteArrayAsync().Result;
var outStream = new MemoryStream();
using (var gzStream = new GZipStream(userGzipByteArray , CompressionMode.Decompress))
{
gzStream.CopyTo(outStream);
}
var userByteArray = outStream.ToArray();
var userJson = userByteArray .ConvertToString();
var user = JSON.Deserialize<User>(userJson , Jil.Options.ISO8601PrettyPrintIncludeInherited);

You can use the AutomaticDecompression flag for this. See Does .NET's HttpWebResponse uncompress automatically GZiped and Deflated responses?

Related

Using C# HttpClient to POST File without multipart/form-data

I'm trying to interact with a API that doesn't support multipart/form-data for uploading a file.
I've been able to get this to work with the older WebClient but since it's being deprecated I wanted to utilize the newer HttpClient.
The code I have for WebClient that works with this end point looks like this:
using (WebClient client = new WebClient())
{
byte[] file = File.ReadAllBytes(filePath);
client.Headers.Add("Authorization", apiKey);
client.Headers.Add("Content-Type", "application/pdf");
byte[] rawResponse = client.UploadData(uploadURI.ToString(), file);
string response = System.Text.Encoding.ASCII.GetString(rawResponse);
JsonDocument doc = JsonDocument.Parse(response);
return doc.RootElement.GetProperty("documentId").ToString();
}
I've not found a way to get an equivalent upload to work with HttpClient since it seems to always use multipart.
I think it would look something like this
using var client = new HttpClient();
var file = File.ReadAllBytes(filePath);
var content = new ByteArrayContent(file);
content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
var result = await client.PostAsync(uploadURI.ToString(), content);
result.EnsureSuccessStatusCode();
var response = await result.Content.ReadAsStringAsync();
var doc = JsonDocument.Parse(response);
return doc.RootElement.GetProperty("documentId").ToString();
What speaks against using simply HttpClient's PostAsync method in conjunction with ByteArrayContent?
byte[] fileData = ...;
var payload = new ByteArrayContent(fileData);
payload.Headers.Add("Content-Type", "application/pdf");
myHttpClient.PostAsync(uploadURI, payload);

Convert HttpWebRequest to HttpClient with POST method

I try to convert HttpWebRequest to HttpClient but without success.
Can anybody help me?
It is my simple code with HttpWebRequest:
string url = "https://www.somesite.com/Service";
string postData = "text to send";
var data = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Proxy = null;
request.AllowAutoRedirect = false;
request.UserAgent = "Mozilla/5.0";
request.ContentType = "text/x-gwt-rpc; charset=UTF-8";
request.Headers.Add("Cookie", SetCookie);//get it after login
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseText = reader.ReadToEnd();
I think you can convert you HttpWebRequest based code to HttpClient based like this:
string url = "https://www.somesite.com/Service";
string postData = "text to send";
var data = Encoding.ASCII.GetBytes(postData);
var content = new ByteArrayContent(data);
using var httpHandler = new HttpClientHandler { UseCookies = false, AllowAutoRedirect = false };
using var client = new HttpClient(httpHandler);
client.DefaultRequestHeaders.Add("UserAgent","Mozilla/5.0");
client.DefaultRequestHeaders.Add("ContentType", "text/x-gwt-rpc; charset=UTF-8");
client.DefaultRequestHeaders.Add("Cookie", SetCookie);
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, url) { Content = content };
var response = await client.SendAsync(requestMessage);
var responseText = await response.Content.ReadAsStringAsync();
Remarks:
Instead of writing the Request's Stream manually you can use the ByteArrayContent abstraction for this. (Related SO topic)
In order to set the cookie(s) manually you have to turn-off the default behaviour. You can do this via the HttpClientHandler's UseCookies. (Related SO topic)
To set the headers manually you can use the HttpClient's DefaultRequestHeaders (Related SO topic)
The counterpart of GetResponse is the SendAsync
Instead of reading the Response's Stream manually you can use the HttpContent's ReadAsStringAsync (Related SO topic)
UPDATE: Include OP's amended code
var content = new StringContent(postData, Encoding.UTF8, "text/x-gwt-rpc");
So, instead of ByteArrayContent StringContent is being used.

C# Binary File Post Request

I am dealing with a third-party API which insists on a binary File Upload request being formatted without a Content-Type header value of multipart/form-data, and with the following headers:
Content-Type: application/octet-stream
Content-Disposition: filename*=UTF-8''file.zip
HttpRequestMessage and HttpContent.Headers.ContentDisposition.DispositionType won't allow me to achieve this either because I can't set the values as desired or they set them automatically.
I accept that this API may not be following HTTP Standards but it's not mine and I have no influence over it.
My attempt which does not work
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.ExpectContinue = false;
FileStream fs = new FileStream(#"e:\dev\TestHalfB.docx", FileMode.Open);
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, <Uri>);
HttpContent fc = new StreamContent(fs);
var mpContent = new MultipartFormDataContent();
mpContent.Add(fc);
fc.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
req.Content = fc;
fc.Headers.ContentDisposition.DispositionType = "filename*=UTF-8''TestHalfB.docx";
using (var response = await client.SendAsync(req))
{
response.EnsureSuccessStatusCode();
var resp = await response.Content.ReadAsStringAsync();
}
fs.Close();
}
Does anyone know of a lower level API I could use or have any suggestions?
So the crux is how can I set the Content-Disposition header to the value I desire.
I had to switch to using WebRequest.
WebRequest request = WebRequest.Create("https://cloud.memsource.com/web/api2/v1/projects/{id}/jobs?token={token}");
request.Method = "POST";
byte[] byteArray = File.ReadAllBytes(#"E:\Dev\TestHalfB.docx");
request.ContentType = "application/octet-stream";
request.ContentLength = byteArray.Length;
request.Headers.Add("Content-Disposition", "filename*=UTF-8''TestHalfB.docx");
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
((HttpWebResponse)response).StatusDescription.Dump();
Could you please try this.
HttpContent fileStreamContent = new StreamContent(paramFileStream);
using (var formData = new MultipartFormDataContent())
{
formData.Add(fileStreamContent, "file1", "file1");
var response = client.PostAsync(actionUrl, formData).Result;
}
This should work for you (or at least get you started, since it's not tested):
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, <Uri>);
using (FileStream fs = new FileStream(#"e:\dev\TestHalfB.docx", FileMode.Open))
{
byte[] fb = new byte[(int)fs.Length]; // assumes your file size will fit into an int
await fs.ReadAsync(fb, 0, (int)fs.Length);
req.Content = new ByteArrayContent(fb);
req.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
req.Content.Headers.ContentDisposition.FileNameStar = "UTF-8''TestHalfB.docx";
using (var response = await client.SendAsync(req))
{
response.EnsureSuccessStatusCode();
var resp = await response.Content.ReadAsStringAsync();
}
}

Cannot download a pdf with RestSharp?

I have been struggling to download a simple pdf hosted online using restsharp. I have been playing around with the code for over an hour and all I get are null object results.
The file downloads easily in POSTMAN using a GET and no content header set but still what gives?
Below is the noddy sandbox test I have been experimenting around with:
[TestFixture]
public class Sandbox
{
[Test]
public void Test()
{
var uri = "https://www.nlm.nih.gov/mesh/2018/download/2018NewMeShHeadings.pdf";
var client = new RestClient();
var request = new RestRequest(uri, Method.GET);
//request.AddHeader("Content-Type", "application/octet-stream");
byte[] response = client.DownloadData(request);
File.WriteAllBytes(#"C:\temp\1.pdf", response);
}
}
Update: Return a Stream
var baseUri = "https://www.nlm.nih.gov/mesh/2018/download/";
var client = new RestClient(baseUri);
var request = new RestRequest("2018NewMeShHeadings.pdf", Method.GET);
request.AddHeader("Content-Type", "application/octet-stream");
var tempFile = Path.GetTempFileName();
var stream = File.Create(tempFile, 1024, FileOptions.DeleteOnClose);
request.ResponseWriter = responseStream => responseStream.CopyTo(stream);
var response = client.DownloadData(request);
The stream is now populated with the downloaded data.
Try this:
var uri = "https://www.nlm.nih.gov/mesh/2018/download/";
var client = new RestClient(uri);
var request = new RestRequest("2018NewMeShHeadings.pdf", Method.GET);
//request.AddHeader("Content-Type", "application/octet-stream");
byte[] response = client.DownloadData(request);

Twilio enhanced answering machine detection c#

How exactly do I use the enhanced AMD through Twilio? I understand that it can only be done through the REST API (no TwiML) but I'm having a hard time seeing the connection between a standard call and the answering machine detection.
I've read through this page a few times, but I still don't understand. So here is the standard c# code for placing a call through the REST API:
TwilioClient.Init(AccountSid, AuthToken);
var to = new PhoneNumber("+14155551212");
var from = new PhoneNumber("+15017250604");
var call = CallResource.Create(to, from, url: new Uri("http://demo.twilio.com/docs/voice.xml"));
And here is my c# translated code from the aforementioned link:
using (var client = new HttpClient())
{
var byteArray = Encoding.ASCII.GetBytes($#"{AccountSid}:{AuthToken}");
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.DefaultRequestHeaders.Authorization = header;
var requestContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("To", "+15017250604"),
new KeyValuePair<string, string>("From", "+15017250604"),
new KeyValuePair<string, string>("MachineDetection", "DetectMessageEnd"),
new KeyValuePair<string, string>("Url", Url.Action("PostTransfer"))
});
var response = client.PostAsync(_amdRequest, requestContent);
var responseContent = response.Result.Content;
}
So what am I missing? I'm sure it's something simple, but I'm not seeing how the enhanced AMD knows what call to listen to, and what the order of events here should be. And finally, how am I supposed to see the results?
EDIT:
So to be crystal clear, here is my code as it currently sits:
TwilioClient.Init(AccountSid, AuthToken);
var toPhone = new PhoneNumber(to);
var fromPhone = new PhoneNumber(from);
var call = CallResource.Create(toPhone, fromPhone, url: new Uri("http://demo.twilio.com/docs/voice.xml"));
using (var client = new HttpClient())
{
var byteArray = Encoding.ASCII.GetBytes($#"{AccountSid}:{AuthToken}");
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.DefaultRequestHeaders.Authorization = header;
var requestContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("To", to),
new KeyValuePair<string, string>("From", from),
new KeyValuePair<string, string>("MachineDetection", "DetectMessageEnd"),
new KeyValuePair<string, string>("Url", Url.Action("PostTransfer"))
});
var response = client.PostAsync(_amdRequest, requestContent);
var responseContent = response.Result.Content;
}
And elsewhere in my code is a function called "PostTransfer" that gets the "AnsweredBy" parameter and does some stuff after the call is placed. Should this be working? Because it isn't. The call goes through and I can hear Twilio's example file play, but it never gets to the "PostTransfer" function.
Twilio developer evangelist here.
You look like you're making a call with detection successfully. You're making a call from your Twilio number to a users number.
You see the results of the answering machine detection when Twilio has decided on whether it is a machine or a human, at which point it makes a webhook request to your URL that you send as part of making the call.
When Twilio makes the webhook, it will include an extra parameter: AnsweredBy. When you set MachineDetection to DetectMessageEnd the values of AnsweredBy can be: machine_end_beep, machine_end_silence, machine_end_other, human, fax, and unknown. You can then read this value and decide what to do with the call at this point.
Does that help at all?
Can you try HttpWebRequest instead? here is an example how to do it,
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = "POST";
request.Headers.Add("Authorization", string.Format("Bearer {0}",AccessToken));
request.ContentType = "application/json;charset=utf-8";
request.ContentLength = body.Length;
request.Accept = "application/json"
if (!string.IsNullOrWhiteSpace(body))
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(body);
request.ContentLength = bytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
// Send the data.
requestStream.Write(bytes, 0, bytes.Length);
}
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (callback != null)
{
var reader = new StreamReader(response.GetResponseStream());
callback(reader.ReadToEnd());
}
}

Categories