Given the following method in a controller for a ASP.NET Web API:
[HttpGet]
[ApiRoute("resource/{id}/end-point)]
public IHttpActionResult MethodName (int id, string clientTimeZone)
{
...
}
Whenever I submit a GET request to http://localhost:5684/api/v1/resource/1/end-point?client_timezone=%2B0500 clientTimezone is passed in to clientTimeZone as %2B0500 and it parses the encoded '+' sign into a space character. Why can't ASP.NET decode +'s from the URI?
In the header I have "ContentType = application/json" and a bearer token
I am trying to get "+0500" into my method but it is turning into " 0500"
Are you using Content-Type of application/x-www-form-urlencoded when consuming the api? This will treat a '+' character as a space when used in your URL.
More details here:
When to encode space to plus (+) or %20?
Try changing your Content-Type to application/json instead and see if parameter binding behaves as expected.
Related
In my controller I have an HTTP get method that accepts a string
[HttpGet("{token}"]
public async Task<IActionResult> GetFoo(string token)
{
//Some actine
return Ok(object);
}
If I send the below-encoded token test%2Atest, ASP .NET will decode this token to test*test by default. But if I send test%2Ftest, it does not decode the %2F to /.
I can understand why ASP.NET doesn't do that as it breaks the routes.
Is there a way to disable this default behavior so I can de the decoding in my controller?
Several test results in my side, but I don't think it's what you want.
I think if you can use this kind of parameter-receiving method?
https://localhost:44393/api/hello?token=test%2FParam
[HttpGet]
public string Index(string token)
{
return token;
}
I could not find a way to disable default URL decoding in ASP.net core.
As Tiny Wang suggested above, I changed the application to send the token in the header and that fixed the problem.
For anyone facing something similar, looking for a more "elegant" solution: just base64 encode the parameter before the request and decode it when you have to use it. I faced the same issue when sending an auto-generated email confirmation token through email and later an http request, and enconding it in base64 and decoding it when using it did the trick just fine.
I am trying to send a large string to c# controller. controller signature looks like this:
public async Task<IHttpActionResult> Post([FromBody] string longString)
I am getting longString as null. I am doing the same in Postman and see that if turn off the default Content-Length header which says: "calculated when request is sent" and add the same header with a value that equal the length of the string + 8 the back end gets correct value as it does when default Content-Length header is on. My POST request from Angular has everything the same except the value of Content-Length header. It is equal the length of the string + 6. So, I am trying to change Content-Length header value to the one that works in Postman like this:
headers = new HttpHeaders().set('Content-Type', 'application/json;');
headers.set('Content-Length', '140317');
But Network shows that the above change is not taking a place.
Any idea how get it work?
Thanks
I am curently integrating the api to my application. One of the parameter is content_url which is a valid HTML URL for content. How to I pass whole url with query string?
API Endpoint parameter is located below.
var contentUrl
= "https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml/OSMANLIKP/FXBULTEN/2020-01-20/null";
var url = "https://campaigns.zoho.com/api/v1.1/createCampaign?" +
"resfmt=json&" +
"campaignname=Campaign12&" +
"from_email=mert.metin#finnet.gen.tr&" +
"subject=Test Create Campaign 11&" +
"list_details=%7Bd6ef220cc42b50d7ddf3236cbb9b8847894375f7a7d2d66f%3A%5B%5D%2Cd6ef220cc42b50d71438a8b019c635e5894375f7a7d2d66f%3A%5B%5D%7D&" +
"content_url=" + contentUrl;
Content url is https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml?SiteKod=OSMANLIKP&Kod=FXBULTEN&Tarih=2020-01-20
But I post url value, API gets the url until first & ampersand character. So remain parameter was missing. I need to pass whole url as a parameter.
Expected Result=https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml?SiteKod=OSMANLIKP&Kod=FXBULTEN&Tarih=2020-01-20
Actual Result=https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml?SiteKod=OSMANLIKP
My application is based on MVC and C# language. I also enclosed the url with double quotes but it is not work because of Api Response which is INVALID IMPORT URL.
You must encode the URL before passing as a parameter as
myURL="myURL1?redir=" + Server.URLEncode("myURL2?id=4&user=test");
It will create a correct URL without '?' and "&" characters as
myURL= myURL1?redir=myURL2%3id%34%26user%3dtest
To extract your redirect url from this encoded url you must use
HttpServerUtility.UrlDecode to turn it into a correct url again.
I am new in developing Rest service using web api in asp .net .
I try to send image byte with in the query string.but i could not abe to send image byte with in query string.
I have Basic concept in GET And Post.
Post don't have any limitation to send data.
Herewith i have added my sample code also.
[AcceptVerbs("GET", "POST")]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RjFGavoTPDMAKffBe3K5Hudx19CDBqnXz5Gi+XV7C0uoa1JOkWswjKccbnX+LCwDBzgB10tw6ATuxrCjxGtMrNMp2yBR4j2wJGgcbNstOngKeUj309I+F7lHm6c1S/AF1i//Qspl8tYJkASkplTRoBktyf2Sbbr3C/ifm/VQrmeEuPOpcj3GzK4HH0Is/G4BkPkVI+Ppl+PmAS7brpfo+39OIX5t9KmYsL359Cx8Mn6JuewdDsHEafvWCfMHG7y9/LdoMzc2iZfIKrg3dZbEDoCLn0Kx0RWFgGj0NmOWy5LI/yMqWiIlubjF4YsO30aQCYz76fl/DJiEOeSvh2EsRunKQrPtNzG7/0DiRtv21uc5qJ6mO0nSSu5ViFTB6r88hNcUIz7WqZPM4pE/ACqjzKWxd1FHgChcAhqiSgSDmU9AGluEC+S/JZIDLTZH/pO3qporKf4V41eKoQdHPkQvJ40sfbATxVHuUAxduuQAKHAYi4RUn4ShGoURhgFAok+5TxWQHOONb2czjiu2nFkxwe1U7yeDJs5mF5lE+qVHgARxhqB/e31R9zsA1SvvemwVOKR/BuETxWIquUCqtU7FaNnKohDSU0+5nqeWyzX0rglRO8aubyNosFbGOMHVRNX9seBTtJwZPSd1WLJ8HFpuIRPA9TKr4gI1v2VfTN2aObY+cHIWXwqjR4+oHbxQNnGrzN4CKXwYVb+ngET6pU/EwJqLFFh0W3GrpdQCdKbhm8CoLHqNYtoxd1ETX/QoO3y5tgZ5ca79pSBk8pHmecsVhAg6fBs+wBrIHH4TKleA3wEDxvDXN5XFlAJq6oyS3x6Nb/rwOwdxlIC3gcAtLgadWzpHqpg0
url:
http://localhost:64528/api/controller/PostRoute?routingRequestTemplate=string
following error I got
Request filtering is configured on the Web server to deny the request because the query string is too long.
Querystring have limition of size, below is details
Maximum length of Querystring is based on the browser not depend upon the asp.net.
Maximum length of a querystring in IE 4.0 and above is ~2048 characters
IE. 4,5,6,7, - 2,048 characters.
Opera supports - 4050 characters.
Netscape 6 supports - 2000 characters.
Firefox Supports - 6000 characters
Try below codes with Post verb :
[HttpPost]
public ActionResult PostRoute(HttpPostedFileBase file) {
}
this link will also help you :
Uploading image in ASP.NET MVC
What is the maximum possible length of a query string?
Your image will need to be quite small, and base64 encoded, or made url safe. URL length is quite limited. I would suggest putting it in a POST body.
http://en.wikipedia.org/wiki/Base64
I have an ASP.NET MVC web site. One of my routes is a URL that takes 5 parameters. For the sake of illustration, these parameters are named parameter1, parameter2, parameter3, parameter4, and parameter5. Currently, I'm constructing a URL in some C# code that will POST to the mvc action via a WebClient. that code looks like this:
WebClient myWebClient = new WebClient();
myWebClient.UploadStringCompleted += myWebClient_UploadStringCompleted;
string url = "http://www.example.com/customer/" + parameter1 + "/orders/" + parameter2 + "/" + parameter3 + "/" + parameter4 + "/" + parameter5;
myWebClient.UploadStringAsync(new Uri(url, UriKind.Absolute));
I'm confident that the UploadString method does a POST. I need to do a POST, because my parameter values can be very long. In fact, I estimate that occasionally, the total url length may be 20000 characters long. Regardless, I get a 400 error when I attempt to post my data. In an effort to debug this, I'm trying to figure out how to simulate a POST in Fiddler.
Assuming that I am passing values via a query string as shown above, what values do I enter into Fiddler? From the Composer tab, I'm not sure what to enter for the Request Headers area. I'm also not entirely sure what to enter for the url. I'm not sure if I put the entire URL in there, including the parameter values, or if those belong in the Request Headers.
What I need to enter into Fiddler, so that I can debug my issue?
Basically all your parameters are a part of the URL, and this is the root of your problem. Here is what is going on: you are hitting the URL length limitation, and receiving a "400 Bad request" error. In real world most web browsers do not work with URLs more than 2000 characters long.
To resolve this problem I would suggest doing a bit of refactoring, so that request is posted to the URL http://www.example.com/customer/parameter1/orders or even http://www.example.com/customer/orders with parameters send in request body. Here is how test such request in Fiddler:
On Composer tab choose POST request verb
Specify the URL as
http://www.example.com/customer/parameter1/orders
or
http://www.example.com/customer/orders
In Request Headers section you can set content type header like
Content-Type: application/x-www-form-urlencoded
or any other header you might require. Or just leave it blank which will work in your case.
Finally in Request Body field list your parameters in query string form
parameter1name=parameter1value¶meter2name=parameter2value
In this new case here is how you can send such a request using WebClient:
WebClient myWebClient = new WebClient();
myWebClient.UploadStringCompleted += myWebClient_UploadStringCompleted;
string url = "http://www.example.com/customer/orders";
string data = "parameter1name=parameter1value¶meter2name=parameter2value";
myWebClient.UploadStringAsync(new Uri(url, UriKind.Absolute), data);
I simply mimic the exact request that was sent.
This is how I do it:
Open Fiddler
Go to the page that I want to re-issue the command i.e. repeat the bug step but watch for the request in the list
Select it from the list and right-click, go to replay > reissue and edit
This build a replicated request but hits a break point before it is sent (You will see the red bar on the right)
Above this you can edit the values that were sent by double-clicking on any of them in Headers, QueryString etc
Then hit Run to Complete