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.
Thank you.
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.)
Here you go, you’re looking for the file testLogin2.xaml file.
Though you have imported the namespace, try appending System.Web before Httpcontext
Regards,
So I tried creating a new project in UiPath named “testLogin3”, and now I’m having problems with methods used by the RestSharp library:
image876×403 17.5 KB
I installed the WebAPI packages from UiPath, and imported the RestSharp and System.Web namespaces.
I’m so sorry, but could you please take a look and check what might be wrong? testlogin3.zip (66.9 KB)
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. image710×206 10.7 KB
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 :
image862×401 18.5 KB
Arguments image703×204 7.88 KB
Exception
Sorry @ptrobot , forgot to add the altered line of code you suggested:
How can I make sure that there is content in the response headers?
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());
cookie = "No cookie for you!";
image727×242 6.43 KB