Reach Web Api Action with ampersand in query string - asp.net-web-api

how can I reach my Web Api Actions with ampersand in the query string?
This works:
http://localhost:12345/api/MyController/MyAction?user=test&pw=abc123
This does not:
http://localhost:12345/api/MyController/MyAction?user=test&pw=abc123
Error Message is:
{
"message": "No HTTP resource was found that matches the request URI 'http://localhost:12345/api/MyController/MyAction?user=test&pw=abc123'.",
"messageDetail": "No action was found on the controller 'MyController' that matches the request."
}
I thought Web API would automatically decode the query string parameters but apperently it does not...

When you use & in the query string, you are actually escaping that ampersand so that it gets read as part of that parameter. So the Web API method is actually only receiving one parameter, "user" that has "test&pw=abc123" in it.

Related

Lambda Proxy integration queryStringParameters special characters

I'm in the midst of testing a new AWS SAM application, and I found a bug that i'm having trouble solving.
I have a route with a simple GET request
/myRoute
and this route support query params such as "mail"
so the full url looks something like this
/myroute?mail=mymail#example.com
aws is creating the proper event for me my problem lies when I have special characters inside my string params for example mymail+bonzo#example.com than what I'll actually get after aws-sam interpret the call is "mymail bonzo#example.com" with a space instead of the special character +.
Any ideas?
This isn't an issue with SAM, Lambda, or API Gateway. The + symbol indicates a space (see this answer on that subject). You should URL encode your query string parameters, and decode them in your Lambda. The + symbol is %2B.

Azure Functions proxy to url provided by querystring gives 404

I've set up an Azure Functions proxy (using proxies.json). This should just pick the value given in the original request's url query string parameter and use that as a value for backendUri. So the goal is that the response of the call to the proxy contains the response of calling the URL that's in the url query string parameter directly. I need this because of CORS.
Here's my proxies.json
{
"$schema": "http://json.schemastore.org/proxies",
"proxies": {
"proxy1": {
"debug": true,
"matchCondition": {
"methods": ["GET"],
"route": "/proxy/"
},
"backendUri": "{request.querystring.url}"
}
}
}
When I call the proxy using https://not-an-actual-url.azurewebsites.net/proxy/?url=https://stackoverflow.com I'm getting back a 404. Same if I encode the value of the url parameter. If I set the backendUri in proxies.json to a static URL instead of trying to use the query string, it works, however.
To summarize, I want the value of backendUri to depend on the URL of the original request. As stated in the docs this should be possible. Quote from the docs:
Set the backend URL to another endpoint. This endpoint could be a function in another function app, or it could be any other API. The value does not need to be static, and it can reference application settings and parameters from the original client request.
When I call the proxy using
https://not-an-actual-url.azurewebsites.net/proxy/?url=https://stackoverflow.com
I'm getting back a 404. Same if I encode the value of the url
parameter. If I set the backendUri in proxies.json to a static URL
instead of trying to use the query string, it works, however.
Judging from your problem description, you don't seem to have a real HttpTrigger. You want to use function app as a server to forward requests to an address, right?
I think it is unrealistic that you want to dynamically get the url from the request and apply it to proxies.json. Because this file is already loaded when the function app is started, you cannot let the requested information enter, it will read your value as a normal string, if it is not a direct url, it cannot be read.
For CORS, you can find some free and public servers for forwarding, or build a server for forwarding by yourself. The proxies.json of function app may not realize your idea.

How to handle invalid/extra special characters & = in request url-SpringBoot?

I have a Rest service where get call if I send multiple invalid/extra & and = characters then also my endpoint does not throw any error. I would like to throw back invalid request error if url contains any extra special character like & or =.
for example:
http://localhost:8080/myservice?rollNo=03456789321&school=Myschool //This is Okay for me
http://localhost:8080/myservice?rollNo=03456789321&school= //should throw error as school is not having value
http://localhost:8080/myservice?rollNo=03456789321&&&&school=Myschool
//should throw error as &&&& is multiple where it should only one
http://localhost:8080/myservice?rollNo=03456789321&= //should throw error as &= is there at end having no sence.
Note that , I am hitting these request from postman , and I have doubt that postman do something with these parameters, cause I am not able to find these extra characters in spring boot while debugging.
Any way through which i can get whole request url in my controller so that I can find out for these charecters comming?
Any built in springboot annotation is there to handle such a cases?
I got my problem solved.
After lot of research , and some observation I came to know that when you pass any number of characters among & and = in request url, the rest client tools like postman , or advanced rest client will refine the url before hitting actual server and remove those extra un-necessary characters. SO if you write multiple &&&& or == charecters in url , it will consider each extra & as blank parameter and will ignore while sending final request, only of those characters which has parameter names besides it it will taken as part of refined request.
you can see in screenshot bellow:
You can Use #RequestParam in your Spring Boot rest Controller
Something of the following
#GetMapping(value = "/myservice")
public boolean doSomething(#RequestParam("rollNo") Integer rollNo , #RequestParam("school") String school) {
doValidation(rollNo,school);
// Do Something
return true;
}
#RequestParam will make sure that your Url need to have these Params rollNo & school. Without it it will throw error.
But if you were to pass an empty string like &school= in your second example. The controller will get an empty String.
You can add a basic validation layer right before you do anything in you controller to handle this condition.

Url in a path variable spring restful service

When I am passing email address as path variable it is throwing following error
Console --> 2015-02-09 16:30:06,634 WARN - GET request for "http://localhost:8181/abc/users/testabghtmail#gmail.com" resulted in 406 (Not Acceptable); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 406 Not Acceptable
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:607)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:565)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:521)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:439)
at RestClient.main(RestClient.java:35)
I have tried lots of cases, so I finally found the problem with last domain like .com and .org which are internationalize domains. So instead of "testabghtmail#gmail.com" if I pass "testabghtmail#gmail.dom" it will work perfectly fine.
My code is
#RequestMapping(value = "users/{emailId:.*}", method = RequestMethod.GET)
public Object searchUser(#PathVariable("emailId") String emailId){
logger.info("Inside search user --> emailId " + emailId);
return userService.findUserByuserId(emailId);
}
I found no answer to this. I think it's an http rule we can't have domains at last in prameters and can make a request.
So work around to this is just pass a slash at the end of the url and there you go.
Like modify "http://localhost:8181/abc/users/testabghtmail#gmail.com/" with "http://localhost:8181/abc/users/testabghtmail#gmail.com". And thanks to spring rest architecture, it will automatically omit the last slash and you will get "testabghtmail#gmail.com" as a parameter value.
Let me know if you guys find something else.

Flex 4 - Sending string (such as JSON) using HTTPService

When I use HTTPService.send(paramter) as a POST request, the web server does not appear to see variable "parameter" if it is a string. The server sees the parameter if it's an Object, but I'm looking to use something like httpservice.send(JSON.encode(object)); Is this possible?
Why not use the actual request objects.
in your service define request objects and post them or send them as get if you please.
Sample code here: http://pastebin.com/ft7QW2vg
Then just call .send on the service.
on the server you can simlpy process if with request.form (Asp)
Failing which why not append it to the url with a binding expression. (you would need to encode it since you would be more or less faking a url or a get behaviour).

Resources