I have the following url http://example-api.com/userid/1234
How can I added the 1234 path parameter to my URL.
the RestRequest class has AddParameter and ParameterType but I do not see any option for path parameter
RestRequest.AddParameter(value, ParameterType.);
Use AddUrlSegment()
https://restsharp.dev/usage.html#url-segment
var myParamValue = "1234"
var request = new RestRequest("http://example-api.com/userid/{myParam}")
.AddUrlSegment("myParam", myParamValue);
Related
I've just started using .netcore.
I need to create a url, I've come across UriBuilder however I can only see example of using that with named paramters e.g.: localhost?value=abc&another=def, however I want to construct without parameter names, so:
localhost/abc/def/
Is this possible, I was thinking maybe something like the below:
var request = new HttpRequestMessage();
request.RequestUri = new Uri(_myUrl);
request.Method = HttpMethod.Get;
var uriBuilder = new UriBuilder(request.RequestUri);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query[0] = "abc"
Any help appreciated.
That's not how UriBuilder or URIs work. What you are trying to change/set is the path of the URI. That is done using the Path property, or using the relevant constructor of UriBuilder. Your example shows how to change/set the Query-String (key-value pairs, basically).
How you create the path is not the business of the UriBuilder class (see #Irdis comment about an option).
Basically, you can create your path manually using string manipulation of your choice, then assign it to the Path property, e.g.:
var b = new UriBuilder(...);
// Set other properties to your requirements
b.Path = "/the/path/ optional with spaces and other stuff/";
You can then use the UriBuilder.Uri property to get the full URI or use the UriBuilder.ToString() method to get the properly escaped URI. For the above example that would be something like:
http://localhost/the/path/%20optional%20with%20spaces%20and%20other%20stuff/
I have a GET end point which will take user name as parameter. Below is the action
[Route("user/{userName}")]
public User GetUserByName([FromUri] string userName)
{
// logic here
}
This is how i make the request.
var restClient = new RestClient("uri");
var request = new RestRequest("user/" + userName);
var response = restClient.Execute(request);
It worked fine for all cases till a user with name containing forward slash came.
Eg: Akbar/Badhusha
Then the request will looks like user/Akbar/Badhusha
This causing the request to return Not Fount error
I tried to add the parameter with AddQueryParameter method. All returning Not found error.
I also tried HttpUtility.UrlEncode
Also tried replacing / with %2f
Also tried user?userName=Akbar/Badhusha
All of them failed.
Is there any way to make it work?
Try removing [FromUri] from the parameter as shown below,
[Route("user")]
public User GetUserByName(string userName)
{
// logic here
}
And the request may look like,
user?userName=Akbar/Badhush
I've tried to logout from my session using GET request from IdentityServer4 docs.
HttpResponseMessage looks like this:
HttpResponseMessage res = await client.GetAsync($"connect/endsession?id_token_hint={idTokenHint}&post_logout_redirect_uri={postLogoutRedirectUri}");
At first, I have a problem with Uri lenght. When I send request, method catch exception
Invalid URI: The Uri scheme is too long.
To fix this problem I've tried send parameters into string like this:
var parameters = $"?id_token_hint={idTokenHint}&post_logout_redirect_uri={postLogoutRedirectUri}";
HttpResponseMessage res = await client.GetAsync("connect/endsession" + parameters );
Also add MaxRequestLineSize in Program.cs like this:
UseKestrel(options =>
{
options.Limits.MaxRequestLineSize = 20480;
})
Also tried this way: https://stackoverflow.com/a/32457474/9541386 But nothing working for me.
I've tried send this request by Postman. Request was sent
http://localhost:5000/connect/endsession?id_token_hint={idTokenHint}&post_logout_redirect_uri={postLogoutRedirectUri}
but in FindClientByIdAsync method from IClientStore interface clientId parameter looks like this:
But in normal case there is Id. I can't see what happens before it because it's first entry point.
How can I fix problem with Uri length and wrong parameter?
The problem is most likely an invalid character in the query parameters:
?id_token_hint={idTokenHint}&post_logout_redirect_uri={postLogoutRedirectUri}
In this case I suspect the string postLogoutRedirectUri that contains the character :, which is invalid if not escaped: %3A.
Encode the uri:
var encodedUri = System.Web.HttpUtility.UrlEncode(postLogoutRedirectUri);
And use this as parameter:
?id_token_hint={idTokenHint}&post_logout_redirect_uri={encodedUri}
While that may fix the problem, why don't you use the provided methods to signout? E.g.:
//using Microsoft.AspNetCore.Authentication;
//using Microsoft.AspNetCore.Mvc;
// Remove cookie
await HttpContext.SignOutAsync("Cookies");
// Signout oidc
await HttpContext.SignOutAsync("oidc");
I have a URL like this:
http://www.example.com/Catalog/Category/Detail
http://www.example.com/Catalog/Products/12
Now, I want to extract the /Catalog/Category/Detail and /Catalog/Products/12 part so I can append with some other base url. How can I do that easily?
Use Uri class, and use Uri.LocalPath property like:
Uri uri = new Uri("http://www.example.com/Catalog/Category/Detail");
Console.WriteLine(uri.LocalPath); // /Catalog/Category/Detail
var segments = new Uri("http://www.example.com/Catalog/Category/Detail").Segments;
This will return
/
Catalog/
Category/
Detail
Uri url = new Uri("http://www.website.com/content/a/?filter=porn");
Is there any way only get a string with "/content/a/" from the Uri?
I mean no domain or query string parameters without having to work with strings?
Not sure why #AlexK deleted his answer, but url.AbsolutePath will give you that info.
Uri url = new Uri("http://www.website.com/content/a/?filter=porn");
Console.WriteLine(url.AbsolutePath);
// outputs /content/a
https://dotnetfiddle.net/3koJ7v