Jax rs optional header param - jax-rs

I am using dropwizard and jax rs to build my simple webapp. I read that all the query params specified in my method definition are optional and so are the HeaderParams. But here is where I am facing problem.
When I skip passing any query param, still the request is routed to the appropriate method. But when I skip passing header param, my request is not even routed to that method. Kindly help.
Example)
#GET
#Path(/sample)
Public Response getSample(#HeaderParam("header") final String header,
#QueryParam("query") final String query) {
}
In this sample, if I call this method with "header", this method gets invoked irrespective of whether I pass "query" param or not. At the same time, if I don't pass the "header", this method is not even getting invoked.
Also even if I give curl -H "header:" "http://localhost/sample?query=dummy" the request is not getting routed. I have to necessarily give a value for my header. Kindly help.
Note: I wrote down simplified representation of my code. Internally I am using dropwizard and swagger.
Thanks,
Sriram

Related

How to get an api get request's query parameter using Playwright-Java

Context :
Trying to intercept and Replace a browser get request's query string parameters using playwright java.
I am able to print the URL of the get request along with the query string parameters using page.onRequest
Issue:
Please help me find a method in the request object that could retrieve the query string parameters in the payload section or any other way to intercept and Replace a browser get request's query string parameters.
PFB Playwrights available methods in the request object
PFB the Network tab log of the fired get request

JMeter - RegEx Extractor seems correct but request header has ${token} instead of value

Set-up
Request #1
POST https://url/
RegEx Extractor
Response #1
{
"Token":"WkQTxNnZRR0nofyJzb-kioALlXgwc7cN9rokXrKzWmtB2BDedUXeQnd94S5KWvaz0",
"ExpirationUTC":"2121-09-17T14:39:57.504Z",
"TokenId":"string"
}
Request #2
GET https://some-other-url
Header Manager
As shown:
Result
Instead of
Authorization: Bearer WkQTxNnZRR0nofyJzb-kioALlXgwc7cN9rokXrKzWmtB2BDedUXeQnd94S5KWvaz0
we have
Authorization: Bearer ${token}
Debug Component Results - Starting to Look Like a Variable Scope Issue
... but:
Result of RegExp Tester
Here, I used the same reg-ex as I used in the Reg Ex Extractor, and it finds the desired string.
JSON Extractor Attempt
Still says Bearer ${token}
As per JMeter Documentation Variables are local to a thread hence you cannot refer the variables which are set in one Thread Group in another Thread Group.
You either need to convert the variable into a JMeter Property using __setProperty() function in 1st thread group and load it using __P() function in 2nd thread group or go for Inter-Thread Communication Plugin
Also be informed that JSON is not a regular language hence using regular expressions is not the best choice for extracting the token from the response

Why do I need to use a Postman pre-request for variables to work in the request body?

In Postman 7.13.0 on Ubuntu, I have variables in one of my collections. They are defined at collection level, not global level, and work perfectly everywhere except in the request body of my POST requests. For example:
"{{name}}" in the request body ends up as "" (the variable is not being substituted). This throws 500 errors from my API as it is expecting a value to be passed. The request I'm looking at is within the same collection that has the variable.
However, if I use a pre-request script like the following, the collection variables work as expected.
var vm_name = pm.variables.get("vm_name");
var cluster_uuid = pm.variables.get("cluster_uuid");
var cluster_name = pm.variables.get("cluster_name");
postman.setEnvironmentVariable("vm_name", vm_name)
postman.setEnvironmentVariable("cluster_name", cluster_name)
postman.setEnvironmentVariable("cluster_uuid", cluster_uuid)
Also, when creating the request body, typing {{ does not begin the autocomplete sequence I'm used to seeing when using the variables elsewhere. This suggests a scope issue although I'm sure that's being done correctly.
I've looked at the Postman docs and I'm sure the scope is correct.
What am I doing wrong that means I must use a pre-request script?
Thanks

How to set http response code in Parse Server cloud function?

A parse server cloud function is defined via
Parse.Cloud.define("hello", function(request, response) {..});
on the response, I can call response.success(X) and response.error(Y), and that sets the http response code and the body of the response.
But how do I define a different code, like created (201)?
And how do I set the headers of the response?
thanks, Tim
You are allowed to return any valid JSON from response.success(). Therefore, you could create an object with fields such as code, message, and value, so you can set the code, give it a string descriptor, and pass back the value you normally would, if there is one. This seems to accomplish what you need, though you will have to keep track of those codes across your platforms. I recommend looking up standard http response codes and make sure you don't overlap with any standards.

Unable to add body to RestSharp RestRequest using enums

I am using RestSharp in ASP .NET MVC 2 project. Trying to create RestRequest (using POST method) and add two enum values (my enum type -- OrderStatusFlags) to request body -- using build-in RestSharp XmlSerializer:
var request = new RestRequest("orders/{vendorID}/{number}", Method.POST);
request.AddBody(previousOrderStatus);
request.AddBody(newOrderStatus);
But after calling AddBody method in request parameters can see only empty but no value. And while calling MVC action method an error occurs:
The parameters dictionary contains a null entry for parameter 'previousStatus' of non-nullable type 'OrderStatusFlags' for method 'RestResponse PostOrderStatus(Int32, System.String, OrderStatusFlags, OrderStatusFlags)' in 'OrdersResourceEndpoint'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Enum look like this:
public enum OrderStatusFlags : long
{
Pending,
Confirmed,
...
}
Does anybody occurs a similiar situation?
A couple issues here. First, you can only call AddBody() once or the last call will take precedence. AddBody() is also only for sending XML as the request body. What is the required XML schema that you need to send to that URL? Can you post some sample XML that you're trying to generate?
I think more likely you actually want to use AddParameter() to add some POST parameters since that is far more common than XML request bodies.