Web API parameter accept any parameter after valid one - asp.net-web-api

I have API that have one parameter string UserId a but while testing it accept any parameter after the parameter i have defined like this
http://localhost:xxxx/api/get?UserId=43141688 its shows the result I wanted and i have one parameter that is UserId but if I do like this
"http://localhost:xxxx/api/get?UserId=43141688&xyzpqr and also gets the same result on the basis of UserId
and &xyzpqr is not valid
so how to do parameter binding basis of my parameter no additional (? and & )include only UserId?
What I have tried:
I already tried attribute based routing and action-based routing

Related

How to use key and value in RS Tag for API testing

Where do we mention the body parameters like key and value in RS Tag for API testing?
There is a param tag under rs using param tag you can pass the parameter name and values.
param is a sub-tag used with “rs” tag if the webservice request requires parameters to be passed. Attributes “name” and “value” are used to store the name and value of the parameters.

Optional Input parameters in CRM action always NULL in Code Activity even if not passed?

If I ommit an optional input parameter in a CRM action call, will this parameter always be null in the Code Activity?
We have a customer which calls a particular CRM action, lets say an update action. The customer wants to be able to pass input parameters as null if the value in the corresponding field in dynamics must be deleted.
the problem i an facing now is that I cannot detect wether the input variable was effectively passed like "parameter_1 = null" or if the parameter itself was not even passed in the action call. the issue is that I cannot delete the value in crm if the input parameter was just not passed. only if the parameter was passed with the value null, I am allowed to delete the field value in crm.
Am I correct assuming that the value of a optional action input parameter is also null if the parameter is not passed at all?
Is there maybe a workaround which enables me to detect wether the value of the input parameter was passed as null instead of beeing ommitted?
something like "undefined" or similar?
You are correct. Input parameters are always present in the IPluginExecutionContext.InputParameters collection passed to the plugin handling your action.
You would need an extra "MyParameterNameSpecified" so to speak to signal if the parameter null value was actually passed.
Another option could be to use a string parameter holding the value passed in JSON-serialized form.

Capture a dynamic value generated for request.1 and pass it to another request

I am trying to pass a URL with a specific parameter having dynamic value for eg. email value will be
${__Random(0000,9999)}+$tester#testing.com.
This generate a random value+email id and get passed for request no.1.
I want to get that exact value for the email id parameter which get passed for that request and again pass that value to another request.
Use third parameter in __Random to save random value:
A reference name for reusing the value computed by this function.
${__Random(0000,9999, rnd)}+$tester#testing.com
And then use the variable concatenated with email suffix later:
${rnd}+$tester#testing.com

#RequestHeader required property behavior for request paramter and value

Can we make a header parameter mandatory but not the value using #RequestHeader?
For example if we use,
#RequestHeader(value = "abc", required = true)
both parameter and it value has to be there.
Edit:
Suppose i call some rest api has above request header param with "abc" but no value. So in this case i am able to invoke the rest api successfully since i have invoke with "abc" header param even i did not enter a value to it. Due to some governance tool rule, i need to have a specific header param but i dont want force user to enter any value.
Spring 5.2 and lower
The #RequestHeader doesn't provide additional facility to check the value of the parameter to be mandatory i.e. not null.
Given below are the available fields as per Spring Doc
defaultValue: The default value to use as a fallback.
name: The name of the request header to bind to.
required: Whether the header is required; null values are allowed
value: Alias for name()
So what you can do is read the parameter either with the help of #RequestHeader or inject a HttpServletRequest request, read by request.getHeader(...) and check inside the controller method if the value exists and then can call methods to perform the necessary logic.
Although you can make sure that the parameter exists with the help of required attribute for e.g. #RequestHeader(value = "Authorization", required = true) String authorization).
Spring 5.3+
The required field was tightened up, both the property and the value should exist. From the release notes:
The #RequestHeader annotation detect a null conversion result value and treat it as missing. In order to allow an empty value to be injected as a null argument, either set required=false on the argument annotation, e.g. #RequestParam(required=false), or declare the argument as #Nullable.
As #Mudassar said, #RequestHeader doesn't provide additional facility to check the value of the parameter to be mandatory i.e. not null.
It is related to this issue: https://github.com/spring-projects/spring-framework/issues/23939
I developed a workaround for this problem using an annotation #RequestHeaderNonNull that I've created: https://github.com/armandoalmeida/request-header-required-non-null
I hope I've helped you!

Passing extra parameter through GetAll method of webapi

How to pass an extra parameter through a Get method of webapi because when i pass
GetALL(int page,int limit,int start) it works fine but when in passed one more parameters that is optional and may be null it throws error.
GetAll(int page,int limit,int start,string ? search)
What is the best way to make it working
In Web API optional parameters are those which can be nulled.
If you have type values like int or DateTime, you need to make them nullableby using the ? syntax.
But when they're classes instead of value type, they are directly convertible to null, so you don't need to, and can not, mark them as nullable. SO, your method signature must be simply this:
GetAll(int page,int limit,int start,string search)
If you wanted page, limit or start to be nullable, you should declare them as int?. So, int he signature above this 3 parameters are compulsory, and the last one optional.
EDIT, for OP comment
When you use the default routing for Web API the only way to choose the right method is by parameter matching, i.e. the parameters in the request must match the parameters in the action including the optional parameters. So, there are two ways to make it work:
post the optional parameters as empty parameters. For your case, provided you're using the query string, include a &search= in the URL
modify the routes, so that the parameters are provided as route parameters, and define the search parameter as optional
You can also completely modify the web API routing by including the action in the route. In that case, you have to specify the action in the URLs to invoke the action, but the method can be chosen by action name (given by method name or Action attribute), and not by parameter matching. In that case you don't need to provide the optional parameters. It will work like MVC routing.

Resources