MVC set cookie not getting full string - cookies

My cookie will not set the full string "A&B". When I request it, I only get
"A". Do I need to wrap it with something so it reads the "&", as I wonder if this character is breaking it.
controller:
Response.Cookies["Order"]["PrincId"] = model.Princ_ID.ToString();
view:
var session_princid = Request.Cookies["Order"]["PrincId"];
#session_princid

Related

Modify a cookie in JMeter to include curly braces around the value

I have the following JSR223 PostProcessor where the captured c_QUID value is to be surrounded by curly braces and used in a cookie. For example if c_QUID is 5575-9878-4848-8897, then I need to set the cookie as {5575-9878-4848-8897}. What can I modify in the below script to do that? As of now it does not add the curly braces.
import org.apache.jmeter.protocol.http.control.*
//Get cookie manager
CookieManager cm = sampler.getCookieManager()
log.info("XXXXXXXXXX QUID " + vars.get("c_QUID"));
Cookie c = new Cookie("QUID", vars.get("c_QUID"), "stage.randomtesting.com", "/", true, 1557578515)
cm.add(c);
Use string concatenation the value:
new Cookie("QUID", "{" + vars.get("c_QUID") + "}",
Shouldn't it be something like:
Cookie c = new Cookie("QUID", "{" + vars.get("c_QUID") + "}", "stage.randomtesting.com", "/", true, 1557578515)
By the way, you might want to use current timestamp as the cookie expiration date because your value of 1557578515 is in the past so the application might not accept it, you might want to replace it with something in the future? See Creating and Testing Dates in JMeter - Learn How article for more details
Also wouldn't that be easier just to define the custom cookie in the HTTP Cookie Manager?

Progress 4GL GET request with cookie

I have to send some data to API but autorization to this api is thru cookie. So for sending data I have to send my username and password and in response I get cookie and in response body I get element "sessionID" which containts string value for that cookie. In every next API request I need to use that cookie (or create it with stored string value from first API request). How to make/integrate cookie functionality inside my existing procedures for API requests? If I do requests using Postman application, then it automaticly saves cookie from first request and use it on every next request.
So is solution for my problem :
to save a cookie and use it for every next request?
to save string value and create cookie for every next request?
to store cookie into session and use cookie from session?
something else I didn't thought of?
My procedure for sending GET request is below.
USING System.Xml.* FROM ASSEMBLY.
USING System.Net.* FROM ASSEMBLY.
USING System.Collections.Specialized.* FROM ASSEMBLY.
ROUTINE-LEVEL ON ERROR UNDO, THROW.
/* *************************** Definitions ************************** */
DEFINE INPUT PARAMETER cLink AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER cUser AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER cPass AS CHARACTER NO-UNDO.
DEFINE OUTPUT PARAMETER lcResponse AS LONGCHAR NO-UNDO.
DEFINE VARIABLE wcClient AS System.Net.WebClient NO-UNDO.
DEFINE VARIABLE nvValues AS System.Collections.Specialized.NameValueCollection NO-UNDO.
DEFINE VARIABLE whcResponse AS System.Net.WebHeaderCollection NO-UNDO.
DEFINE VARIABLE rsResponse AS "System.Byte[]" NO-UNDO.
DEFINE VARIABLE cRespHeader AS CHARACTER NO-UNDO.
DEFINE VARIABLE wpProxy AS System.Net.WebProxy NO-UNDO.
/* *************************** Main Block *************************** */
wcClient = NEW System.Net.WebClient().
nvValues = NEW System.Collections.Specialized.NameValueCollection().
ASSIGN
nvValues["username"] = cUser
nvValues["password"] = cPass
.
rsResponse = wcClient:UploadValues(cLink, nvValues).
lcResponse = System.Text.Encoding:Default:GetString(rsResponse).
cRespHeader = System.Text.Encoding:Default:GetString(rsResponse).
DELETE OBJECT nvValues.
DELETE OBJECT rsResponse.
DELETE OBJECT wcClient.
Found some C# code for cookies but don't know how to integrate that into my procedure.
CookieContainer cookieContainer = new CookieContainer();
System.Net.Cookie userNameCookie = new System.Net.Cookie("user", "username");
System.Net.Cookie passwordCookie = new System.Net.Cookie("password", "9848jf7s7ejhd");
cookieContainer.Add(userNameCookie);
cookieContainer.Add(passwordCookie);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(produkt);
request.CookieContainer = cookieContainer;
Thanks in advance
Solution for this problem is next
wcClient:Headers:ADD("Cookie","value_of_cookie_including_name").
Spent lots of hours testing and finally it worked. Hope that this answer someone saves a lot of time.

Adding a cookie to DocumentRequest

Using AngleSharp v 0.9.9, I'm loading a page with OpenAsync which sets a bunch of cookies, something like:
var configuration = Configuration.Default.WithHttpClientRequester().WithCookies();
var currentContext = BrowsingContext.New(configuration);
// ....
var doc = context.OpenAsync(url, token);
This works fine and I can see the cookies have been set. For example, I can do this:
var cookieProvider = currentContext.Configuration.Services.OfType<ICookieProvider>().First() as MemoryCookieProvider;
And examine it in the debugger and see the cookies in there (for domain=.share.state.nm.us)
Then I need to submit a post:
var request = new DocumentRequest(postUrl);
request.Method = HttpMethod.Post;
request.Headers["Content-Type"] = "application/x-www-form-urlencoded";
request.Headers["User-Agent"] = userAgent;
//...
Which eventually gets submitted:
var download = loader.DownloadAsync(request);
And I can see (using Fiddler) that it's submitting the cookies from the cookieProvider.
However, I need to add a cookie (and possible change the value in another) and no matter what I try, it doesn't seem to include it. For example, I do this:
cookieProvider.Container.Add(new System.Net.Cookie()
{
Domain = ".share.state.nm.us",
Name = "psback",
Value = "somevalue",
Path = "/"
});
And again I can examine the cookieProvider in the debugger and see the cookie I set. But when I actually submit the request and look in fiddler, the new cookie isn't included.
This seems like it should be really simple, what is the correct way to set a new cookie and have it included in subsequent requests?
I think there are two potential ways to solve this.
either use document.Cookie for setting a new cookie (would require an active document that already is at the desired domain) or
Use a Filter for getting / manipulating the request before its send. This let's you really just change the used cookie container before actually submitting.
The Filter is set in the DefaultLoader configuration. See https://github.com/AngleSharp/AngleSharp/blob/master/src/AngleSharp/ConfigurationExtensions.cs#L152.

How to build a Postman url query with Pre-request Scripts

I'm trying to use a pre-request script to build out a request object based on data pulled from a CSV file. The problem is that the request seems to be set in stone prior to the pre-request script being run. That would seem to make this a mid-request script almost rather than a pre-request.
My code is as follows:
if(ipList === undefined) ipList = "1.2.3.4,2.3.4.5,123.234.345.465";
let ips = ipList.split(',');
let queryArray = [];
for( i=0; i<ips.length; i++){
queryArray.push({ "key": "ip", "value": ips[i] });
}
console.log(queryArray);
pm.request.url.query = queryArray;
console.log(pm.request);
When I hardcode a url query variable in the request to equal 4.3.2.1, the pm.response.url object like this:
pm.request.url.query[0] = {key:"ip", value:"4.3.2.1"}
Note that the url.query[0] part of the object matches the parameter in the actual get request.
When I change the value of pm.request.url.query to equal the new query array, however as you can see here, the query array is set correctly, but the parameters are not appended to the request URL.
So unless I'm doing something wrong, it appears that the request is immutable even to the pre-request scripts.
So my question is this:
Is there a way to modify the url params of a request prior to making the request?
BTW: I know that is might seem odd to have multiple params with the same key in a query, but that's the way this API works and hard coding multiple ip addresses in the query works just fine.
You could just assign a new value to pm.request.url.
Here I had some query params already in the URL, which I had to edit:
const urlSplit = request.url.split('?');
const paramsString = urlSplit[1]; // the second part actually represents the query string we need to modify
const eachParamArray = paramsString.split('&');
let params = {};
eachParamArray.forEach((param) => {
const key = param.split('=')[0];
const value = param.split('=')[1];
Object.assign(params, {[key]: value});
});
params.bla = params.bla + 'foobar';
newQueryString = Object.keys(params).map(key => key + '=' + params[key]).join('&');
pm.request.url = urlSplit[0] + '?' + newQueryString;
In the end, I just constructed a new URL, using the first part of the previous one & the query string with the edited bla parameter.
This seemed to work for me--it didn't change what the UI shows the query string is, but it changed what the actual request was (looking at the console log)
pm.request.url.addQueryParams(["a=1", "b=2"])
pm.request.url.query.remove("b")
I have some parameters called "script_loginAs" etc... named such that people on my team know the parameter is evaluated and not sent.

output variable stored in database

I'm storing the page content in a database table. The page content also includes some CF variables (for example "...this vendor provides services to #VARIABLES.vendorLocale#").
VARIABLES.vendorLocal is set on the page based on a URL string.
Next a CFC is accessed to get the corresponding page text from the database.
And this is then output on the page: #qryPageContent.c_content#
But #VARIABLES.vendorLocale# is showing up as is, not as the actual variable. Is there anyway to get a "variable within a variable" to be output correctly?
This is on a CF9 server.
If you have a string i.e.
variables.vendorLocal = 'foo';
variables.saveMe = 'This is a string for supplier "#variables.vendorLocal#'"' ;
WriteOutput(variables.saveMe); // This is a string for locale "foo"
then coldfusion will attempt to parse that to insert whatever variable variables.vendorLocale is. To get around this, you can use a placeholder string that is not likely to be used elsewhere. Commonly you'll see [[NAME]] used for this purpose, so in this example
variables.saveMe = 'This is a string for supplier "[[VENDORLOCALE]]'"' ;
WriteOutput(variables.saveMe); // This is a string for supplier "[[VENDORLOCALE]]"
Now you've got that you can then later on replace it for your value
variables.vendorLocal = 'bar';
variables.loadedString = Replace(variables.saveMe,'[[VENDORLOCALE]]',variables.vendorLocal);
WriteOutput(variables.loadedString); // This is a string for locale "bar"
I hope this is of help
There are lots of reasons storing code itself in the database is a bad idea, but that's not your question, so I won't go into that. One way to accomplish what you want is to take the code you have stored as as string, write a temporary file, include that file in the page, then delete that temporary file. For instance, here's a little UDF that implements that concept:
<cfscript>
function dynamicInclude(cfmlcode){
var pathToInclude = createUUID() & ".cfm";
var pathToWrite = expandPath(pathToInclude);
fileWrite(pathToWrite,arguments.cfmlcode);
include pathToInclude;
fileDelete(pathToWrite);
}
language = "CFML";
somecfml = "This has some <b>#language#</b> in it";
writeOutput(dynamicInclude(somecfml));
</cfscript>