var client = new RestSharp.RestClient();
client.BaseUrl = new Uri(url);
var request = new RestSharp.RestRequest(RestSharp.Method.POST);
client.FollowRedirects=false;
request.AddCookie("Cookie", "Cookie_1=value; JSESSIONID=node0gi4uhegtzmqevx6bn233abhg344007.node0");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("Payload", payload);
IRestResponse response = client.Execute(request);
cookie = HttpContext.Current.Server.UrlDecode(response.Headers.ToList().Find(x => x.Name == "Set-Cookie").Value.ToString());
Console.WriteLine(cookie);
The output result should be the cookie from the response named âSet-Cookieâ. However, when I execute the code it errors out with the exception âThe name âHttpContextâ does not exist in the current contextâ.
I have double checked and the namespace âSystem.Webâ is present in the Imported namespaces panel, but the problem persists:
Any tips on this case?
In alternative, is there an easier way to do REST API calls where FollowRedirects is set to false? From what I understand, the standard HTTP activity from the Web.API package does not have this functionality.
No compiled code to run
error CS0234: The type or namespace name âHttpContextâ does not exist in the namespace âSystem.Webâ (are you missing an assembly reference?) At line 12
Would you mind sharing your workflow? Your code doesnât generate the same error on my end.
Usually when I get the error CS0234 and I know I have imported everything correctly, the workaround is to just declare a variable in the Variables or the Arguments panel. (You donât need to do anything with the variable.)
Please copy your original code and paste in your code again in the Invoke Code activity. C# is case sensitive. When you have the language set to VB.NET at first and paste the code, UiPath changes the case automatically (e.g. new to New, false to False). This generates errors even though you change the language to C# later on.
Also change Payload to payload and Cookie to cookie.
OK, resolved the code errors and changed names of Cookie to cookie and Payload to payload, but when executing the activity it still errors out with CS0103 :
Nevermind, the expression is correct, I changed the value to another expected response header and it accessed the value no problem, which means that itâs now a problem with the invocation of the request.
You can assign the header to a variable and check if itâs null first. Also I donât believe you can use HttpContext.Current. It seems to always be null unless you are inside a web application. Microsoft recommends that you use WebUtility.UrlDecode() instead.
var h = response.Headers.ToList().Find(x => x.Name == "Set-Cookie");
if (h != null)
{
cookie = System.Net.WebUtility.UrlDecode(h.Value.ToString());
}
else
{
cookie = "No cookie for you!";
}