I have a thin frontend API web service that does some preprocessing on the received data and then sends the data to my backend API web service using HttpClient.
There are some complex cases when a request contains multipart data with JSON and files, and I don't want to parse it at all in the frontend. The backend will do the job.
So, I would like to take the request "as is" - as raw as possible (not caring about its contents and whether it's multipart or not) and just forward it to the backend API.
I tried the following:
var msg = new HttpRequestMessage(HttpMethod.Post, resourceUrl);
msg.Content = new StreamContent(request.Body);
var apiResponse = await _httpClient.SendAsync(msg);
but the backend web service receives an empty request body with 0 length.
How do I forward the entire request body without having to analyze it and reassemble a new request body?
You need to set msg.Content.ContentLength for this to work correctly. It's also a good idea to copy ContentType and other content headers from the request into msg.Content, so that your backend service knows how to parse it.
Related
In Aps.net Core Web API I have Request.Body
var rawRequestBody = await new StreamReader(Request.Body).ReadToEndAsync().ConfigureAwait(false);
In asp.net MVC I have Request.Content
Stream stream = await Request.Content.ReadAsStreamAsync().ConfigureAwait(false);
I am trying to upload the file as a stream. Please suggest how I can get the file as stream with Request.Body in asp.net core web api controller.
I am testing the upload file using postman.
Thanks !
In http a body of request can have headers, some times called content or payload headers. These describe certain attributes of the body.
In regards to C#
Body is a Stream, and is just a chunk of data representing the body of the request.
Stream
Provides a generic view of a sequence of bytes
Content is a HttpContent. Content can have extra information like headers to describe the Body.
HttpContent
A base class representing an HTTP entity body and content headers
You can read the data (of the body) from the Content as a Stream (as you have shown).
You can read more about headers here
HTTP headers
HTTP headers let the client and the server pass additional information
with an HTTP request or response. An HTTP header consists of its
case-insensitive name followed by a colon (:), then by its value.
Whitespace before the value is ignored.
I'm trying to Post an OFX request to a bank server with RestSharp. I have succeeded in getting the desired transaction data from the server. However, when the request body exceeds 1024 bytes, an Expect header is being added to the request. Normally, I'd just avoid the complexity and try to keep the request under 1024 bytes, but that is unavoidable with a few of the queries I want to run.
Unfortunately, the server does not support expect headers and throws an error if it receives one. I have been unable so far to identify how to prevent RestSharp from adding the Expect header. The code I'm using looks roughly like what I have below:
var client = new RestClient("https://bankwebsite.com");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-ofx");
request.AddHeader("Accept", "*/*, application/x-ofx");
request.AddParameter("application/x-ofx", ofxString, ParameterType.RequestBody);
var response = client.Execute(request);
Is there a setting I'm missing? Is this intrinsic to how these requests are formed? I'm relatively new to working with HTTP, so I'm most likely missing something. I've tried setting ServicePointManager.Expect100Continue to false, which didn't change any behavior.
Using HttpClient instead of RestSharp has caused huge issues, so I would like to avoid that if I can.
I'm using RestSharp 106.2.1 with .Net Core 2.0 on MacOS.
I'm trying to retrieve inbound messages from the API on Mandrill, but when I call the sendRaw method, the API fails with an error saying I need to specify a raw message value. I guess the SendRaw is sending the message I specify, rather than returning the message I request?
static async Task<string> SendRaw(string key)//,string )
{
string sendRaw = mandrillAPI + "inbound/send-raw.json";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(sendRaw);
HttpResponseMessage response = await client.GetAsync(key);
string s = await response.Content.ReadAsStringAsync();
return s;
}
The raw message is the content I'm trying to retrieve, so I don't see how I can supply it.
Is there a way to retrieve messages from the server using the API? And if that is the wrong way to say it...lets put it this way: I set up a domain with Mandrill and sent a message to a fictional mailbox on that domain. The server relayed the message--I can go on the Mandrill dashboard and see the SendRaw API call for the message. I would like to retrieve the message from where-ever it is located...whether it is located on Mandrill's servers or whether it was sent to my domain where I needed to have something listening for the send, I don't know. I'm very new at this (circa yesterday). Either way, I need to get that message. Is it possible to do it using the API?
Or is Mandrill just relaying the message to my url when it is originally sent, and I need to set up the url to receive the message in order to get it?
Inbound mail can't be retrieved through the Mandrill API. Inbound mail is received by Mandrill, converted to an inbound/send-raw API call, and then POSTed to the webhook that you've specified for your inbound route. Mandrill doesn't store the message contents for API retrieval. Once they're POSTed to your webhook URL, the message is discarded. Note that inbound/send-raw is different than messages/send-raw. The inbound/send-raw API call allows you to mimic what would happen if you sent a message to a route that you've set up (ie, POSTing to the webhook URL).
More information about how inbound mail works and the webhooks can be found in the Mandrill KB: https://mandrill.zendesk.com/hc/en-us/categories/200277247-Inbound-Email-Processing
I have a web service in a RESTful web server (java) which consumes media of type APPLICATION_FORM_URLENCODED and produces media of type MULTIPART_FORM_DATA. Now I'm working on a REST client (C#) and trying to use this web service. I'm using RestSharp as the REST client. My code goes as follows:
RestRequest request = new RestRequest("getDataFileChunkIS", Method.POST);
request.AddParameter("sessionId", sessionId);
request.AddParameter("dataFileId", dataFileId);
request.AddParameter("offset", offset);
request.AddParameter("chunkSize", chunkSize);
request.AddParameter("checksumFlag", checksumFlag);
RestClient client = new RestClient(url);
RestResponse response = (RestResponse)client.Execute(request);
But in this response I'm getting HTTP Status 406 - Not Acceptable. It says "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers." Maybe I'm doing it in a wrong way. So my question is that how can I execute this request whose response will contain MULTIPART_FORM_DATA ?
1) how can I execute this request whose response will contain MULTIPART_FORM_DATA?
request.AddHeader("Accept", "multipart/form-data")
2) how can I read this response header(contains JSON) using RestClient?
See answers to this question. Particularly the third one, which shows how to do it just with .NET 4.5 libraries.
You may need to implement IDeserializer to get access to the raw HttpResponse for consumption.
I'm new to web api and I need to create a server for a client. I have no control over the client - can't change a thing.
The client sends in an html encapsulated json request in a POST body. However, the content-type can vary. What do I need to do to allow my ApiController to process different content-types?
Under the hood, Web Api supports Content Negotiation mechanism to automatically opt the correct formatter based on the header Content-Type in HTTP request.
By default content negotiation supports three formatters: json, xml and form-urlencoded data. If no formatter found, client will receives HTTP error 406 (Not Acceptable).
See more:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/content-negotiation
If you need to allow Web Api support another Content-Type, you can write your own custom formatter:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/media-formatters