How to call Multipart message request in c# - c#

I need to call an endpoint that requires the Request Header and Request Body with Content-type
and content-length and some other property as well which need to End with BOUNDARY.
Request Header
POST https://www.API-URL.com/ HTTP/1.1
Host: www.API-URL.com
Content-type: multipart/mixed; boundary=BOUNDARY
Content-length: 1034
Request Body
--BOUNDARY
Content-type: application/x-www-form-urlencoded
Content-length: 141
AppVersion=1.0&AcceptLicenseAgreement=Yes&ResponseType=application/x-x-pld&VersionNumber=xxx&UserId=xxxx&Password=xxxxx
--BOUNDARY
Content-type: application/x-x-binary
Content-length: 6
020020 // This the string Data which I need to post
--BOUNDARY--
How can I implement a call to this endpoint by using HttpClient?
Thanks in advance

Related

400-Bad request- request header to long

I'm trying to generate pdf,but after like the 5th page header and footer display http error 400-bad request-request header to long. is there anyway to solve this problem? a response header can look like
this
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: // i hid the adress
Cache-Control: no-store, no-cache
Content-Disposition: attachment; filename=Utskrift.pdf
Content-Length: 162568
Content-Type: application/pdf
Date: Tue, 25 Oct 2022 08:57:16 GMT
Expires: -1
Pragma: no-cache
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
The length you are trying to send is very large to be handled. It is like overdosing the request header. What you can do is, you can consider to break it in to chunks (smaller sizes), and send them separately until you gather all the file size on the endpoint you are sending.

Parse an content-type header from http request stream

I have the contents of an http request saved on disk.
It looks like this:
POST http://test.ca/ 1.1
Accept: application/xml
Content-Type: multipart/form-data; boundary="8942d141-7753-45ab-be3a-53021694e70c"
--8942d141-7753-45ab-be3a-53021694e70c
Content-Type: application/xml Content-Disposition: form-data; name=Bundle
...
--8942d141-7753-45ab-be3a-53021694e70c
Content-Type: image/jpg Content-Disposition: form-data; name="image 1 of 2"; filename=Attachment1.jpg; filename*=utf-8''Attachment1.jpg
...
--8942d141-7753-45ab-be3a-53021694e70c--
Normally, if you were processing this at the time of request, the HttpRequest object would be used to get the content type. If I have that request persisted to disk, are there any available classes I can use to handle parsing the ContentType for me?
Everything else can be parsed using the MultipartReader class, but I'm not aware of anything that will handle parsing the request header, given an instance of a stream. If I don't have to roll my own that would be great.

How to make multipart/mixed request containing nested HTTP requests

I am trying to implement creating a multipart request where each part is a HTTP request
To see what I mean here's for example how this Google API works: https://cloud.google.com/dns/api/batch#example (I don't use that API, it's just an example)
I've seen examples using MultipartFormDataContent but I don't understand how to create each part"
--====1340674896===
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1111
POST /contacts/479038 HTTP/1.1
Content-Type: application/json
Accept: application/json
{ "SomeData" : 1 }
The content of each part is I think the serialized string of a request:
GET /contacts/479038 HTTP/1.1
Content-Type: application/json
Accept: application/json
{ "SomeData" : 1 }
So how do I create this? I guess I need to get the serialized version of a HttpRequestMessage?
Can I just serialize request.Content.ReadAsStringAsync())
In WebAPI I see there's this HttpContentMessage which takes a HttpRequestMessage and can serialize it:
var httpMessageContent = new HttpMessageContent(request);
var buffer = httpMessageContent.ReadAsByteArrayAsync().Result;
stream.Write(buffer, 0, buffer.Length);
But I don't have this class available in .NETStandard

How to process a HTTP POST?

I have a HTTP handler which receives a HTTP POST.
HTTP POST message with header is -
POST /ibe/example.com HTTP/1.1
Content-Length: 445
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
Host: 202.177.46.142
charset=UTF-8&param1=val1&param2=val2
Handler in my code is -
var V1 = context.Request["param1"];
var V2 = context.Request["param2"];
But this didn't work.
I think you should be using:
var V1 = context.Request.Param["param1"];
var V2 = context.Request.Param["param2"];
Taken from this example here: http://forums.asp.net/t/1190337.aspx and here How to send data to httphandler

FromBody not binding string parameter

I have an issue similar to ASP.NET MVC 4 RC Web API Parameter Binding Issue, but I'm trying to solve it by using the [FromBody] attribute.
Fiddler reports the following request (excluding irrelevant bits like User Agent String)
PUT http://localhost:82/api/account/shoppinglistitems HTTP/1.1
Host: localhost:82
Connection: keep-alive
Content-Length: 11
Origin: http://localhost:3000
Content-Type: application/x-www-form-urlencoded
Accept: application/json, text/javascript, */*; q=0.01
query=apple
My controller action is
[HttpPut]
public ShoppingListItemWebModel CreateShoppingListItem([FromBody]string query) {
// query is null
}
I could wrap the parameter in a complex type, but that seems like a hack to fix the issue. Or I could put the query in the URI, but that doesn't fit the pattern of the rest of the API. Is it possible to do it this way? If so, where is my mistake?
change your request to be
PUT http://localhost:82/api/account/shoppinglistitems HTTP/1.1
Host: localhost:82
Connection: keep-alive
Content-Length: 11
Origin: http://localhost:3000
Content-Type: application/x-www-form-urlencoded
Accept: application/json, text/javascript, */*; q=0.01
=apple
notice the lack of "query"
If you're using AngularJS instead of jQuery you might want to opt for the following alternative, since AngularJS uses content type application/json by default.
PUT http://localhost:82/api/account/shoppinglistitems HTTP/1.1
Host: localhost:82
Connection: keep-alive
Content-Length: 7
Origin: http://localhost:3000
Content-Type: application/json
Accept: application/json, text/javascript, */*; q=0.01
'apple'
Notice that the value is wrapped in string quotes (either single or double).

Categories

Resources