HttpClient and form parameter names - http

This code:
using (var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(10) })
{
using (var content = new MultipartFormDataContent())
{
content.Add(new StringContent("abc"), "token");
var response = await client.PostAsync("http://localhost", content);
var result = await response.Content.ReadAsStringAsync();
}
}
Generates the following HTTP request:
POST http://localhost/ HTTP/1.1
Host: localhost
Content-Type: multipart/form-data; boundary="4b39ed14-752b-480a-9846-fc0019132d15"
Content-Length: 174
--4b39ed14-752b-480a-9846-fc0019132d15
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=token
abc
--4b39ed14-752b-480a-9846-fc0019132d15--
We have a client who says their WAF is blocking the request because the name parameter should be quoted
Content-Disposition: form-data; name="token"
I have seen some differences in opinion on this:
https://github.com/akka/akka/issues/18788
https://github.com/akka/akka-http/issues/386
Does anyone know what is correct here?

I posted this to https://github.com/dotnet/runtime/issues/72447
Either form is correct, apparently

Related

Microsoft Cognitive Speech Speaker Identification Can't create enrollment

Please help to advise for my issue below.
I try to create an enrollment using sample code from here
private async Task<HttpResponseMessage> MakeRequest()
{
string path = #"<path_to_wav_file>";
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<my_key>");
// Request parameters
queryString["shortAudio"] = "true";
queryString["identificationProfileId"] = "<my_profile_id>";
var uri = "https://westus.api.cognitive.microsoft.com/spid/v1.0/identificationProfiles/<my_profile_id>/enroll?" + queryString;
HttpResponseMessage response;
// Request body
byte[] byteData = File.ReadAllBytes(path);
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(uri, content);
}
return response;
}
and got the response
{StatusCode: 202, ReasonPhrase: 'Accepted', Version: 1.1, Content:
System.Net.Http.StreamContent, Headers: { Pragma: no-cache
Operation-Location:
https://westus.api.cognitive.microsoft.com/spid/v1.0/operations/af54c843-8df9-4511-8d65-4825ebec024d
apim-request-id: 37567cff-d259-4a1d-82fc-9fc884edcfe3
Strict-Transport-Security: max-age=31536000; includeSubDomains;
preload x-content-type-options: nosniff Cache-Control: no-cache
Date: Tue, 08 Jan 2019 07:12:05 GMT X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET Content-Length: 0 Expires: -1 }}
said that
{"error":{"code":"Unspecified","message":"Access denied due to invalid
subscription key. Make sure you are subscribed to an API you are
trying to call and provide the right key."}}
The error message is strange because I used the same Subscription Key that created profile successfully.
I think you should use ("Ocp-Apim-Subscription-Key", "") when make request to
https://api.projectoxford.ai/spid/v1.0/operations/af54c843-8df9-4511-8d65-4825ebec024d

Fineuploader dotnet core

I'm trying to get write Controller (in asp.net core) code to handle my fineuploader requests but it keeps failing to see the request data despite whatever combination of [FromForm] / [FromBody] / "Content-Type": 'application/json' I use. Here is my uploader config in the view page:
var uploader = new qq.FineUploader({
element: document.getElementById("uploader"),
request: {
endpoint: '#Url.Action("AddAttachment", "Util")',
customHeaders: {
"RequestVerificationToken": '#GetAntiXsrfRequestToken()',
"Content-Type": 'application/json'
}
},
And here is my controller code - not alot for now, I just literally want to see some data getting sent.
public JsonResult AddAttachment([FromForm] Object o){
//var x = HttpContext.Request;
//return Json(x);
if(o == null){
return Json("no data");
}else{
return Json(o);
}
}
and her is what I see fineuploader sending to the server via the network tab in the chrome devtools:
------WebKitFormBoundarySQwYoYovQOkoFU1f
Content-Disposition: form-data; name="test"
876
------WebKitFormBoundarySQwYoYovQOkoFU1f
Content-Disposition: form-data; name="qquuid"
9ba04b80-b3d8-4e2d-8068-792dd77253bd
------WebKitFormBoundarySQwYoYovQOkoFU1f
Content-Disposition: form-data; name="qqfilename"
dohPlayDat.PNG
------WebKitFormBoundarySQwYoYovQOkoFU1f
Content-Disposition: form-data; name="qqtotalfilesize"
3535659
------WebKitFormBoundarySQwYoYovQOkoFU1f
Content-Disposition: form-data; name="qqfile"; filename="dohPlayDat.PNG"
Content-Type: image/png
------WebKitFormBoundarySQwYoYovQOkoFU1f-
Can anyone see the mistake I'm making?
If everything's wired up correctly, you should be able to see your file via Request.Form.Files in the controller.
I was trying to get it as a byte array by action's parameters but had no luck. Try this instead.
The following snippet is an example of how you can bind all the fields from the file upload request, then use the file stream to write the file to a temporary file.
public async Task<IActionResult> Post(string qquuid, string qqfilename, int qqtotalfilesize, IFormFile qqfile)
{
// full path to file in temp location
var filePath = Path.GetTempFileName();
if (qqfile.Length > 0)
{
using (var stream = new FileStream(filePath, FileMode.Create))
{
await qqfile.CopyToAsync(stream);
}
}
}
For more information about using .NET Core to upload files you can have a look over here : https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.1

Display content string from HttpResponseMessage

I have a post controller in an MVC app returning this response:
return new HttpResponseMessage(HttpStatusCode.Accepted)
{
Content = new StringContent("test")
};
When I hit the post URL with this code:
using (WebClient client = new WebClient())
{
string result = client.UploadString(url, content);
}
result contains this response:
StatusCode: 202, ReasonPhrase: 'Accepted', Version: 1.1, Content: System.Net.Http.StringContent, Headers: { Content-Type: text/plain; charset=utf-8 }
Why isn't "test" appearing after Content:?
Thanks!
You should not return HttpResponseMessage from ASP.NET MVC action. In this case you'll get messy response like this:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcRHJvcGJveFxwcm9nXFN0YWNrT3ZlcmZsb3dcZG90TmV0XE12Y0FwcGxpY2F0aW9u?=
X-Powered-By: ASP.NET
Date: Sun, 04 Feb 2018 10:18:38 GMT
Content-Length: 154
StatusCode: 202, ReasonPhrase: 'Accepted', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: text/plain; charset=utf-8
}
As you see, you actually get 200 HTTP response with HttpResponseMessage details in response body. This messy body content is what you deserialize into result variable.
ASP.NET MVC actions should return an instance of the class derived from System.Web.Mvc.ActionResult. Unfortunately, there is no built-in action result that allows setting both return status code and body content.
There is ContentResult class that allows to set return string content with status code of 200. There is also HttpStatusCodeResult that allows setting arbitrary status code but the response body will be empty.
But you could implement your custom action result with settable status code and response body. For simplicity, you could base it on ContentResult class. Here is a sample:
public class ContentResultEx : ContentResult
{
private readonly HttpStatusCode statusCode;
public ContentResultEx(HttpStatusCode statusCode, string message)
{
this.statusCode = statusCode;
Content = message;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
base.ExecuteResult(context);
HttpResponseBase response = context.HttpContext.Response;
response.StatusCode = (int)statusCode;
}
}
The action would look like:
public ActionResult SomeAction()
{
return new ContentResultEx(HttpStatusCode.Accepted, "test");
}
Another possible fix is to change your controller from MVC to WEB API controller. To make this - just change base class of controller from System.Web.Mvc.Controller to System.Web.Http.ApiController. In this case you could return HttpResponseMessage as in your answer.
In both cases you will get correct HTTP response with 202 status code and string in the body:
HTTP/1.1 202 Accepted
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcRHJvcGJveFxwcm9nXFN0YWNrT3ZlcmZsb3dcZG90TmV0XE12Y0FwcGxpY2F0aW9u?=
X-Powered-By: ASP.NET
Date: Sun, 04 Feb 2018 10:35:24 GMT
Content-Length: 4
test

Cannot post object from .net Client web-api web service

I have one .net client which tries to make http request to web api service
here is my Request:
public List<Category> GetCategories()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:54558/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Task<string> response = client.GetStringAsync("api/CategoryApi/");
List<Category> lstCategory = JsonConvert.DeserializeObjectAsync<List<Category>>(response.Result).Result;
return lstCategory;
}
public void Create(Category category)
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var stringContent = new StringContent(category.ToString());
HttpResponseMessage responseMessage = client.PostAsync("api/CategoryApi/", stringContent).Result;
}
and in my webapi
public IEnumerable<Category> GetCategories()
{
return categoryRepository.data;
}
public string PostCategory(Category category)
{
categoryRepository.add(category);
return "MessageOk";
}
SO when I make request to my GetCategories action of the web-api everything is OK.
and no matter what I do it seems that .net application cannot find the Post action of the web-api and I never actually see entering in Postcategory method
as I have also put breakpoints here.
I only get the error
atusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNccG9zdGdyZXNcRGVza3RvcFxXZWJBcGlTZXJ2aWNlXFdlYkFwaVNlcnZpY2VcYXBpXENhdGVnb3J5QXBpXA==?=
Cache-Control: no-cache
Date: Sat, 13 Jun 2015 17:55:16 GMT
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 1022
Content-Type: application/json; charset=utf-8
Expires: -1
}}
What may be the issue. Thak you in advance
You are posting just a Category.ToString which if you didn't override ToString to be a Json or XML string, it will fail on the server side because there is no way to deserialize the content into a Category object. You should serialize the Category on the client before posting it. Also make sure your request headers include the proper Content-Type of application/json. By posting StringContent, the Content-Type won't be application/json. You are setting the Accept header, but that only describes the data coming back to your client, not the data you are posting. One last thing, I would not use the same HttpClient for both the get and the post request. Each method should use it's own HttpClient so you don't have any extra headers depending on the call.

HTTP URLRequest in Adobe Flex

Im trying to make a simple HTTP URLReqest in Adobe Flex, heres the code roughly:
var requestSender:URLLoader = new URLLoader();
var urlRequest :URLRequest = new URLRequest("http://localhost:8888");
var msg:String = "data=blah";
urlRequest.data = msg;
urlRequest.contentType = "application/x-www-form-urlencoded";
urlRequest.method = URLRequestMethod.POST;
This produces something close to:
POST / HTTP/1.1
Referer: app:/PersonSearch.swf
Accept: text/xml, application/xml, application/xhtml+xml, ...
x-flash-version: 10,1,85,3
Content-Type: application/x-www-form-urlencoded
Content-Length: 102
Accept-Encoding: gzip,deflate
User-Agent: Mozilla/5.0 (Windows; U; en-US) ...
Host: 127.0.0.1:8888
Connection: Keep-Alive
data=blah
What I really want is:
POST / HTTP/1.1
Content-Type:application/x-www-form-urlencoded
Connection:close
Via:MDS_777
Accept:*/ *
Host:localhost:8888
Content-Length:104
data=blah
Anyone know how I remove the fields like Accept-Encoding, add fields like "Via" and set Connection to "close"?
Also how do we get the responce from the HTTP request?
Thanks
Phil
The Flash Player doesn't allow you to change the Accept-Encoding or Via headers through ActionScript. If you try do that you will get a message like this one:
Error #2096: The HTTP request header
Accept-Encoding cannot be set via
ActionScript.
If you are using URL variables you can try to simplify your code by doing this:
var variables:URLVariables = new URLVariables();
variables.data = "blah"; //same as doing data=blah
variables.data2 = "blah2"; //same as doing data2=blah2
var requestSender:URLLoader = new URLLoader();
var urlRequest:URLRequest = new URLRequest("http://localhost:8888");
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = variables;
To get the response you will have to listen for the Event.COMPLETE on the "requestSender":
requestSender.addEventListener(Event.COMPLETE, completeHandler);
private function completeHandler(event:Event):void {
// do something with requestSender.data (or URLLoader(event.target).data)
}

Resources