0

So like the title says, I send the bootstrap request and receive 7 cookies back from it. I verified in debugging that first function is getting all 7 cookies, however when Function #2 starts executing, there's only 4 cookies in the collection and the two cookies I actually need are missing (Cookie1 and Cookie2). These functions are part of an ASP.Net project. Is there something I'm missing or do I need to switch to doing the calls asynchronously? One Note, I have used a similar implementation to this for the exact same sequence in another project and didn't have any issues, but that wasn't a ASP project.

How the functions are called:

var cookies = Bootstrap(environmentToWork.BootstrapUri, out errorMsg);
var stepOneCookies = ActivationStepOne(Data1, environmentToWork.ActivateStep1, _activateRequest, out errorMsg, cookies);

Function #1 that gets initial cookies:

private IList<RestResponseCookie> Bootstrap(string uri, out string errorMsg)
    {
        var client = new RestClient(uri)
        {
            Timeout = -1
        };
        IRestRequest request = new RestRequest(Method.POST);
        IRestResponse response = client.Execute(request);
        errorMsg = string.Empty;

        if (!response.IsSuccessful)
            errorMsg = "Bootstrap service failed!";

        return response.Cookies;
    }

Function #2, where cookies are missing:

private IList<RestResponseCookie> ActivationStepOne(string Data1, string uri, string fileName, out string errorMsg, IList<RestResponseCookie> cookies)
    {
        errorMsg = string.Empty;
        var jsonStrings = File.ReadAllText(fileName);
        var returnMessage = string.Empty;
        var getPolicyDetailsJson = JObject.Parse(jsonStrings);
        getPolicyDetailsJson["Data1"] = Data1;
        getPolicyDetailsJson["Data2"] = Data2;
        getPolicyDetailsJson["Data3"] = Data3.Substring(0, 5);
        var client = new RestClient(uri)
        {
            Timeout = -1
        };
        var request = new RestRequest(Method.POST);
        request.AddHeader("Cookie1", cookies.FirstOrDefault(x => x.Name == "Cookie1").Value);
        request.AddParameter(cookies.FirstOrDefault(y => y.Name == "Cookie2").Name, cookies.FirstOrDefault(z => z.Name == "Cookie2").Value, ParameterType.Cookie);
        request.AddHeader("Content-Type", "application/json");
        request.AddParameter("application/json", getPolicyDetailsJson.ToString(), ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);
        if (!response.IsSuccessful)
            errorMsg = "Activation Step 1 Failed";

        return response.Cookies;
    }
Zac Migues
  • 13
  • 2
  • You are not showing how you use or persist the cookies that you get from Bootstrap. We have no way of knowing why you don't have all the cookies you expect. – Crowcoder Apr 29 '22 at 20:28
  • ? store them in an list and pass them into the next function... I'll add that part I guess – Zac Migues Apr 29 '22 at 20:57
  • You still don't show setting them or returning them. You are basically saying that you have made no mistake but yet it doesn't work. You either are not actually putting them in the list or they are not named exactly what you think. – Crowcoder Apr 29 '22 at 21:32
  • @Crowcoder Cookies from Bootstrap response are stored in "var cookies", which I pass into ActivateStepOne, then in ActivateStepOne. When I was debugging after Bootstrap function completed and returned Cookie list to var cookies, it had 7 items in collection. When I stepped into ActivateStepOne, the list had 4 cookies. – Zac Migues Apr 29 '22 at 21:46
  • You did not show the complete `Bootstrap` function of yours; how is it returning the cookies - assumingly return response.Cookies; ? why not the return value of Bootstrap is directly stored in typed variable, like `IList cookies = Bootstrap(` ? – Anand Sowmithiran Apr 30 '22 at 10:12
  • 1
    @AnandSowmithiran Hello, yes I see where I missed to include the return part now, that was my mistake, sorry about that. In the functions, instead of doing "return response.Cookies" I did "IList cookies = response.Cookies; return cookies" and where I call the function, I did it like "IList cookies = Bootstrap(params)". So this question is answered now, thank you! – Zac Migues May 03 '22 at 14:57

1 Answers1

0

Ok so from the comments here I updated the Bootstrap function in original post. I fixed this issue like so:

Calling the functions:

IList<RestResponseCookie> cookies = Bootstrap(environmentToWork.Url1, out errorMsg);
IList<RestResponseCookie> stepOneCookies = ActivationStepOne(Data1, environmentToWork.Url2, _activateRequest, out errorMsg, cookies);

Bootstrap:

private IList<RestResponseCookie> Bootstrap(string uri, out string errorMsg)
    {
        var client = new RestClient(uri)
        {
            Timeout = -1
        };
        IRestRequest request = new RestRequest(Method.POST);
        IRestResponse response = client.Execute(request);
        errorMsg = string.Empty;

        if (!response.IsSuccessful)
            errorMsg = "Bootstrap service failed!";

        IList<RestResponseCookie> cooks = response.Cookies;

        return cooks;
    }

ActivateStepOne:

private IList<RestResponseCookie> ActivationStepOne(string Data1, string uri, string fileName, out string errorMsg, IList<RestResponseCookie> cookies)
{
    errorMsg = string.Empty;
    var jsonStrings = File.ReadAllText(fileName);
    var returnMessage = string.Empty;
    var getPolicyDetailsJson = JObject.Parse(jsonStrings);
    getPolicyDetailsJson["Data1"] = Data1;
    getPolicyDetailsJson["Data2"] = Data2;
    getPolicyDetailsJson["Data3"] = Data3.Substring(0, 5);
    var client = new RestClient(uri)
    {
        Timeout = -1
    };
    var request = new RestRequest(Method.POST);
    request.AddHeader("Cookie1", cookies.FirstOrDefault(x => x.Name == "Cookie1").Value);
    request.AddParameter(cookies.FirstOrDefault(y => y.Name == "Cookie2").Name, cookies.FirstOrDefault(z => z.Name == "Cookie2").Value, ParameterType.Cookie);
    request.AddHeader("Content-Type", "application/json");
    request.AddParameter("application/json", getPolicyDetailsJson.ToString(), ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
    if (!response.IsSuccessful)
        errorMsg = "Activation Step 1 Failed";

        IList<RestResponseCookie> cooks = response.Cookies;

        return cooks;
    }
Zac Migues
  • 13
  • 2