ASP.NET reading content from inner octet/stream - c#

I have HTTP request from some device with body:
--18113960011
Content-Disposition: form-data; name="message"; filename="data"
Content-Type: application/octet-stream
�o��\��Հ��vx�+�#:�țdV+
Iy#o*,cޙ��k
--18113960011--
And I accept this request in ASP.NET Web API controller. How I can read this inner data
�o��\��Հ��vx�+�#:�țdV+
Iy#o*,cޙ��k
as array of bytes using native way (no using substring extracting)?

Related

Sending/Uploading Files through APIs - .Net Core

I would like to send a file through an API. The server will receive the file and save it on the server drive.
The question is that I have two Options to do this:
Read the file as a string and send the whole string in the request Body
Use multi-part
Could someone help in the pros and cons in using the two options.
It's not related to pros and cons, So basically to understand what is multipart means
Multipart requests combine one or more sets of data into a single
body, separated by boundaries. You typically use these requests for
file uploads and for transferring data of several types in a single
request (for example, a file along with a JSON object).
This mean that multipart contains different section of info that separated by boundary(random number).
For example:
POST /upload HTTP/1.1
Content-Length: 428
Content-Type: multipart/form-data; boundary=abcde12345
--abcde12345
Content-Disposition: form-data; name="id"
Content-Type: text/plain
{...Additional plain content goes here...}
--abcde12345
Content-Disposition: form-data; name="address"
Content-Type: application/json
{
"street": "3, Garden St",
"city": "Hillsbery, UT"
}
--abcde12345
Content-Disposition: form-data; name="profileImage "; filename="image1.png"
Content-Type: application/octet-stream
{...file content...}
--abcde12345--
In this example you can see different content sent in one request separated by boundary [boundary=abcde12345]. (Plain text, Json Object and File Content)
The file content section here is used for sending file data in application/octet-stream (string in the binary or base64 format), so basically you are sending the file data in form of string :) as you referred to the first point, but you can include some additional info, may be the new file name to be saved , the user who uploaded this file or whatever you want.
Hope you get the point.
Ref:
https://swagger.io/docs/specification/describing-request-body/file-upload/
https://swagger.io/docs/specification/describing-request-body/multipart-requests/
The only difference on the protocol level is that multipart/form-data requests must adhere to RFC 2388 while a custom typed request body can be arbitrary.
Using Base64 string has an advantage for uploading very tiny individual images. It's easy to handle and avoids dependency on Http.IFormFile. On the contrary, base64 encoded files are larger than the original and you need decode it on the server side.

How to send unencoded form data with C# HttpClient

I'm trying to "repurpose" a third-party API used by a desktop application. I've found that the below code gets me very close to matching the packets sent by the app:
var formData = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>(JsonConvert.SerializeObject(myPayload), "")
});
var response = Client.PostAsync(myURL, formData).Result;
var json = response.Content.ReadAsStringAsync().Result;
This gets me almost exactly the same payload sent by the application, except it encodes the data (I know, "encoded" is right there in the name). I need to get the exact same request but without the data being encoded, but I can't quite find the right object(s) to pull it off. How do I keep this payload from being URL encoded?
Edit:
This is a login request I pulled from Wireshark emanating from the application:
POST /Login HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: 1.1.1.1
Content-Length: 161
Expect: 100-continue
Connection: Close
{"username":"myuser","auth-id":"0a0a140f81a2ce0c303386e93cec41bf04660c22a881be9a"}
This is what the above will generate:
POST /Login HTTP/1.1
Expect: 100-continue
Connection: Close
Content-Type: application/x-www-form-urlencoded
Content-Length: 221
Host: 1.1.1.1
%7B%22user-name%22%3A%22myuser%22%2C%22auth-id%22%3A%220a0a140f81a2ce0c303386e93cec41bf04660c22a881be9a%22%7D=
I've edited them for brevity so the Content-Length is wrong. I realize it might not be the best way to send this data, but I have no control over how it's consumed.
Since you're actually trying to send JSON, I think you need to wrap the JSON in a StringContent object rather than a FormUrlEncoded object. Form-encoded data and JSON data are two different ways of formatting a payload (another commonly used format would be XML, for example). Using them both together doesn't make any sense.
I think something like this should work:
var content = new StringContent(JsonConvert.SerializeObject(myPayload), Encoding.UTF8, "application/json");
var response = Client.PostAsync(myURL, content).Result;
var json = response.Content.ReadAsStringAsync().Result;
(P.S. the Content-Type: application/x-www-form-urlencoded header sent by the application appears to be misleading, since the request body clearly contains JSON. Presumably the receiving server is tolerant of this nonsense, or just ignores it because it's always expecting JSON.)

How to decode an encoded POST data

From a payment provider I get the data as a POST and encoded with
Content-Type: application/x-www-form-urlencoded; charset=utf-8
the issue is that I need to retrieve the data from the
HttpContext.Current.Request.Form
and a simple System.Web.HttpUtility.UrlDecode(context.Request.Form[0])
returns:
"\" transactionRef\"\r\n\r\nd68cafc5d6c8472a893eb0763efca625\r\n------WebKitFormBoundaryKVsW8jF9QdwY35ca\r\nContent-Disposition: form-data; name=\"transactionNumber\"\r\n\r\n10526728\r\n------WebKitFormBoundaryKVsW8jF9QdwY35ca\r\nContent-Disposition: form-data; name=\"orderRef\"\r\n\r\nc7853c2513fd45758b80b95a2dbaf9fa\r\n------WebKitFormBoundaryKVsW8jF9QdwY35ca\r\nContent-Disposition: form-data; name=\"orderId\"\r\n\r\n50043628\r\n------WebKitFormBoundaryKVsW8jF9QdwY35ca\r\nContent-Disposition: form-data; name=\"accountNumber\"\r\n\r\n50250954\r\n------WebKitFormBoundaryKVsW8jF9QdwY35ca\r\nContent-Disposition: form-data; name=\"zzzz\"\r\n\r\nmust be here to get http post to work"
How can I decode the data so I can retrieve each parameter correctly? for example, extract the transactionRef or orderRef?
I did it in a very weird way and I just wonder if there's something easier/failproof that we can use:

I want to upload video on amazon cloud using REST api

I have shared Documentation to upload file from this link.
Upload File
Uploads the file content along with its metadata information.
POST : {{contentUrl}}/nodes?suppress={suppress}
suppress : (Optional)
deduplication: disables checking for duplicates when uploading Body Parameters:
Multi-form part
--------- metadata ------------
name (required) : file name. Max to 256 Characters. kind (required) : "FILE" labels (optional) : Extra information which is indexed. For example the value can be "PHOTO" properties (optional) : List of properties to be added for the file. parents(optional) : List of parent Ids. If no parent folders are provided, the file will be placed in the default root folder.
---------content ------------
File Bytes
Sample Request:
POST /cdproxy/nodes?localId=testPhoto
HTTP/1.1
Host: content-na.drive.amazonaws.com
Authorization: Bearer
Atza|IQEBLjAsAhReYeezFcFdYzqrFOJGv3EG
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="metadata"
{"name":"fooo.jpg","kind":"FILE"}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="content";
filename="db5df4870e4e4b6cbf42727fd434701a.jpg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
**cURL Request:**
curl -v -X POST --form
'metadata={"name":"testVideo1","kind":"FILE"}' --form
'content=#sample_iTunes.mp4'
'https://content-na.drive.amazonaws.com/cdproxy/nodes?localId=testVideo1&suppress=deduplication'
--header "Authorization: Bearer
Atza|IQEBLjAsAhQ5zx7pKp9PCgCy6T1JkQjHHOEzpwIUQM"
Fiddler:
I have written code and sent request.
This is the request i have sent using Fiddler
POST
https://content-na.drive.amazonaws.com/cdproxy//nodes?suppress=deduplication
HTTP/1.1 Authorization: Bearer
Atza|IQEBLjAsAhQXfR4gxrstIKB8jjCbbsTIRhad1QIUZgLJFXgenPSsYLp6VHeL2rEBQ6RFSQYAR847Pvr8bnF4wc-qAlP2oKbAxogI_KJBopMzbTwt3n1DgJ9D8VJTikjdkXH7_595n4ElGhAhKJybhiXBlZLDh7ScXiFC6g4MFfkVRMKoKWAf4gRhhYGTLX2nBSHWIYx5kpzbS0QNgOUS_Hluodfq0j5gf8FeNB3YG6q-KiQMdPbcXJFlKs28f_cSWnQvYyLu0c8YzcJZpjL3CTrabnGpF5KOM6ie71Q2mS3ncL71tEO5wu3MVEJZZtg00MKM5nkGAzH1hCHqA0PWmsLsQVm0X684Seje7aSMYDlh0hcRpgc4Y7fusEndnprz5EO9FftNDN3lbvxszgAR0DhoOg
Content-Type: multipart/form-data Host: content-na.drive.amazonaws.com
Content-Length: 2100825 Expect: 100-continue
------WebKitFormBoundary8d2ff0fafd6a750 Content-Disposition: form-data; name="metadata"
{"name":"C:\Users\jafar.baltidynamolog\Downloads\SampleVideo_360x240_2mb.mp4","kind":"FILE"}
------WebKitFormBoundary8d2ff0fafd6a750 Content-Disposition: form-data; name="content";
filename="C:\Users\jafar.baltidynamolog\Downloads\SampleVideo_360x240_2mb.mp4" Content-Type: video/mp4
��� ftypisom���isomiso2avc1mp41���free��LmdatT Lj����L5
��P�L"�:�ԮoUNx��V���qFM4��w�ח�l�iݧ����o��͑s����z���/e��ۢ�a���G��y�j�W-&{x�w��>�I���6�v�u�j;��pk#�=�K�ZO.a�yy
-w(���ژ�#S�Rpt<{�ڿZ38xt���x�[��ɒ�&�lsT��4P��rMXv��Ѹ%A�5���C�hH& ���P� Z���翷�n�����z���5t��D��M%�}���i���P|�k��#�>�#�tq�O�K���sy��=�\��9���)���Ɏ��������T��(��=v����
)D���R;zKBx��PU� ���E�/bB��)p�h,6��`��� ���XH2
Problem is that i am getting Error 500 internal Error. Kindly help me understand if some thing is missing or wrong. I am using this code to upload video. Kindly help me

Where does strange MIME types come from?

I have a web service for uploading files written in C#.
Front-end application is written in Javascript / HTML5 (using https://github.com/blueimp/jQuery-File-Upload)
Recently, I was reviewing server logs and found some strange MIME types for PDF files that where sent by client browser, for example:
application/unknown
application/force-download
application/force-download/n
application/force-download\n
[application/pdf]
Some of them are causing .NET framework throwing exception:
MultipartMemoryStreamProvider streamProvider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(streamProvider);
"Message Error parsing MIME multipart body part header byte 156 of data segment System.Byte[]."
I don't have a clue what to do with that.
Try checking the Content-Type in the request.
Content-Disposition:
form-data;
name="imagefile";
filename="C:\Users\Pictures\sid.png"
Content-Type:
(notice the blank Content-Type, it should be Content-Type: image/png )
// within WebAPI you can use code below to log the request body
string requestBody = await Request.Content.ReadAsStringAsync();

Categories