Is there a way to avoid amp; prepending to request parameter name in GrailsParameterMap - grails

My request URL is
http://localhost:8080/application/controller/action?param1=value1&param2=value2
I obtain request params in controller through grails-core/WebAttributes.groovy
/**
* Obtains the Grails parameter map
*
* #return The GrailsParameterMap instance
*/
GrailsParameterMap getParams() {
currentRequestAttributes().getParams()
}
The 2nd param in the map is of structure
{LinkedHashMap$Entry#00000} "amp;param2" -> "value2"
How or is it possible to eliminate "amp;"?

Your request URL (https://en.wikipedia.org/wiki/Query_string) includes parameters named amp;param2 and similar; parameters are separated by ampersands, not HTML-encoded ampersands. This is not a problem with Grails, but instead with however you are generating your URL.
How are you generating the request URL that you reference here?

Related

Path parameters being passed as a String with comma not read properly in rest assured

I have a code where I am passing path parameters in the GET request in Rest Assured. But I see the path parameters aren't read properly and I see some gibberish text being read. Actually the String I am passing as path parameter contains a comma in it. Below is my code.
ValidatableResponse response = given().header("Authorization", token).header("Content-type", "application/json")
.when().log().all().pathParam("CalendarId", testCaseBean.getCalendarId().toString())
.queryParam("from", testCaseBean.getStartDate()).queryParam("to", testCaseBean.getEndDate())
.queryParam("monthEnd", testCaseBean.getMonthEndBusinessDay())
.get(EndPoint.GET_CALENDAR_BUSINESS_DAY_INFO_DATE_PARAM).then().log().all();
The path param I am passing is "AUS,EUR" and it is being read as AUS%2CEUR. I am passing this path parameter as test data from the CSV file. Below is the request being formed on the console.
https://portculation-qa.us-east-1.m5435454345.easn.mss.com/master-data/v1/calendars/AUS%2CEUR?from=2022-11-01&to=2022-11-01&monthEnd=false
My expected request URI is https://portculation-qa.us-east-1.m5435454345.easn.mss.com/master-data/v1/calendars/AUS,EUR?from=2022-11-01&to=2022-11-01&monthEnd=false
You can see the only difference in the expected and actual URI is the gibberish path param which isn't read properly. Any solution to tackle this issue?
Try adding this:
.urlEncodingEnabled(false)
RestAssured.given()
.contentType(JSON)
.log()
.all()
.urlEncodingEnabled(false)
or:
RestAssured.urlEncodingEnabled = false;
By default it set to true.

How to test POST method with Postman and x-www-form-urlencoded Body?

I'm at the final step of making my first JS Web App using Pug, where I need to test that it supports POST method, which takes in a string should return a JSON object with 2 key.value pairs that represent original string and its length.
I'm instructed to use Postman and x-www-form-urlencoded to test my App, but I don't know how to test the POST method using urlencoded body.
Can someone tell me what I should fill in for KEY and VALUE on Postman, under x-www-form-urlencoded?
//POST method in route file
router.post('/', (req, res, next) => {
res.render('ps3post', {string: req.body.string, stringLength: req.body.string.length});
});
block content
h1 POST method, page rendered by PUG
p= string
p= stringLength
Okay I figured it out.. It seems that KEY needs to be type (string in my example), and VALUE needs to be what I want the actually value to be.

Proxying MultiPart form requests in Grails

I have a Grails controller that receives a DefaultMultipartHttpServletRequest like so:
def myController() {
DefaultMultipartHttpServletRequest proxyRequest = (DefaultMultipartHttpServletRequest) request
}
This controller acts as a proxy by taking pieces of this request and then resends the request to another destination.
For non-multipart requests, this worked fine, I did something like:
IProxyService service = (IProxyService) clientFactory.create()
Response response = service.doPOST(proxyRequest.getRequestBody())
Where proxyRequest.getRequestBody() contains a JSON block containing the request payload.
However, I do not know how to get this to work with multipart request payload, since the request body is no longer a simple block of JSON, but something like the following (taken from Chrome devtools):
How can I can pass this request payload through using my proxy service above, where doPost takes a String?
Have you tried
def parameterValue = request.getParameter("parameterName")
to get the parameter value?
If you see the method signatures for DefaultMultipartHttpServletRequest you will see there are methods for getting the files and other parameters separately because the request body is getting used to both upload the file and to pass in other parameters.

AFNetworking 2.0 how to put some parameters in the query string of a POST request

I need to call a ws to send an image to a server. Who mades the webserver, decided that the session token should be sent trough the URL and not in the body.
The query should look like: http://api.service.com/imageupload.php?token=434353435 and in the body there should be the image with its parameter.
The session manager when I use the -POST methods puts all the parameter inside the body.
Is there a way to say to the request serializer to put some parameters in the query string and others in the body? or do I need to subclass it?
This can be done like this
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:[#"Google.com" stringByAppendingString:AFQueryStringFromParametersWithEncoding(parameters, NSUTF8StringEncoding)]]] ;
Where parameters is a dictionary

HttpRequest GET with sendData as query parameters?

This question is kind of a duplicate of HTTPRequest.request with sendData, can't seem to get this to work, but I've got some more information now. The goal here is to send a GET request with query parameters attached. I initially tried to send my request as such:
HttpRequest request = new HttpRequest();
request.open("GET", _url, async:true);
request.onError.listen(_onLoadError, onError: _onLoadError);
request.send(sendData);
Where sendData is a String following the normal format for query parameters (?myVariable=2&myOtherVariable=a, etc), since this is the ultimate goal here. The request gets sent, but I never see any additional data (sendData) go with it in any monitoring tools (I'm using Charles). I then tried:
HttpRequest request = new HttpRequest();
request.open("GET", _url + sendData, async:true);
request.onError.listen(_onLoadError, onError: _onLoadError);
request.send();
So now I'm just attaching the query string to the url itself. This works as intended, but is far from elegant. Is there a better solution?
On a GET request you always add the query string to the URL?
When you create the Uri you can pass in a map with the query parameters if you find this more elegant.
Map query = {'xx':'yy', 'zz' : 'ss'};
String url = "http://localhost:8080/myapp/signinService";
Uri uri = new Uri(path: url, queryParameters : query);
HttpRequest.getString(uri.toString().then((HttpRequest req) ...
According to the W3 XMLHttpRequest Specification:
(In the send() method) The argument is ignored if request method is GET or HEAD.
So simple answer to this question is no. sendData cannot be appended to a GET request, this is by XMLHttpRequest specification and not a limitation of Dart.
That said, for requests like this it may be more readable and idiomatic to use HttpRequest.getString
HttpRequest.getString(_url + sendData).then((HttpRequest req) {
// ... Code here
}).catchError(_onLoadError);
If you want to generate a valid URL from url (String) + query parameters (Map) , you can do the following :
Uri uri = Uri.parse(url).replace(queryParameters: parameters);
String finalURL = uri.toString();
url is a String and parameters is a Map

Resources