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

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

Related

I Have a JSON response in string so I used JSR223 processor and extracted the required token

I Have a JSON response in string so I used JSR223 processor and extracted the required token, is there any way to make that variable as global variable for all the threads to use
COuldn't get any slutions or haven't got the right link to check
As per request I have added screenshot for more clarity in my question
enter image description here
I even tried using
$(_setProperty(text3,${token},)};
still no use
I have extracted a string from response and assigned to text3 and I want to make it as global variable in thread group so that It can be used all other next API's I tried using props.put() however it didn't worked

Postman and JSON-Data in URL

I use postman for my API-Tests.
within the environments I define a variable myVariable and its content is JSON:
MyVariable { "var1":"value1", "var2":"value2"}
Can I somehow use this JSON-Values within my request builder in JSON?
You need to do a bit of work as variables are stored as strings and not object.
Assuming you have set your variable at environment level, in your Pre-request Script you need to have the following code:
let var1 = JSON.parse(pm.environment.get('myVariable')).var1;
pm.environment.set('var1', var1);
And then send your request as:
http://my-service/index.html?foo={{var1}}

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.

Jax rs optional header param

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

how to set HTTP_HOST for WebTestCases in Symfony2

My application is generating some absolute links via $this->get('request')->getHost().
Problem is: when I try to run testcases, I get following error message:
[exception] 500 | Internal Server Error | Twig_Error_Runtime
[message] An exception has been thrown during the rendering of a template ("Undefined index: HTTP_HOST") in "::base.html.twig" at line 69.
Somehow it's clear to me that there is no host when calling my app via CLI, but I think there must be a way to prevent Symfony2 from throwing that error.
Anyone knows how to get rid of it?
You could create the request like this:
$request = Request::create('http://example.com/path');
That will make the HTTP host be set.
Maybe what you could do is to inject the host you need directly in the request headers before calling the getter. The host is retrieved by looking at various parameter values. First, the headers parameter X_FORWARDED_HOST is checked to see if it is set. If it is set, it is returned otherwise the method getHost checks if the headers parameter HOST is set then the if the server parameter SERVER_NAME is set and finally if the server parameter SERVER_ADDR is set.
What you could try is to set the header parameter HOST like this before calling the getHost method:
$request = $this->get('request');
$request->headers->set('HOST', 'yourhosthere');
$request->getHost(); // Should return yourhosthere
That being said, I'm not sure this will solve the problem because the error you mentioning tells us that the template tries to retrieve the value of the index HTTP_HOST but it is not defined. Looking at the methods $request->getHost and $request->getHttpHost, I don't see anything trying to retrieve a value having HTTP_HOST as the index but I could have missed it. Could you post the file ::base.html.twig to see if the problem could be lying there.
Regards,
Matt
Thanks guys- your answers lead me into the right direction.
This is no Symfony2 issue, as i figured out:
It's just the facebook API PHP wrapper which directly accesses the SERVER parameters. This code solved my issue:
$facebook = $this->container->get('facebook');
$returnUrl = 'http://'.$request->getHost();
$returnUrl .= $this->container->get('router')->generate('_validate_facebook');
$_SERVER['HTTP_HOST'] = $request->getHost();
$_SERVER['REQUEST_URI'] = $request->getRequestUri();
$loginUrl = $facebook->getLoginUrl(array(
'req_perms' => 'publish_stream',
'next' => $returnUrl,
));
return $loginUrl;
now my app runs from web and CLI again