Search code examples
c#restsharprest

Cookie Header Not Going Through In The Execution of GET Call


Preface:

  • This is a part of my personal project of building a control GUI for several communication devices - using WinForms unfortunately.
  • The part of code running into trouble is rested inside the Constructor of a Form.
  • Additional NuGet packages installed are: Newtonsoft.Json, RestSharp, SpecFlow and SpecFlow.Assist.Dynamic.
  • LoginForm and SeatsInfo are two basic classes created only to store and organize my data, they have no additional coding besides properties declartions.

Execution code:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CONTROL_UI
{
    public partial class RunningConfig : Form
    {
        public RunningConfig()
        {
            InitializeComponent();

            var client = new RestClient("CLIENT_URL");
            var loginRequest = new RestRequest("api/login", Method.POST);

            loginRequest.RequestFormat = DataFormat.Json;
            loginRequest.AddHeader("Content-type", "application/json");
            loginRequest.AddJsonBody(new LoginForm() { username = "admin", password = "", @override = true });

            var loginRespone = client.Execute(loginRequest);

            JObject sessionInfo = JObject.Parse(loginRespone.Content);

            var sessionID = sessionInfo["sid"];

            var seatAvailRequest = new RestRequest("api/seats", Method.GET);

            seatAvailRequest.AddHeader("sid", sessionID.ToString());
            seatAvailRequest.AddHeader("Cookie", "sid = " + sessionID.ToString());

            var seatResponse = client.Execute(seatAvailRequest);

            List<SeatsInfo> seatsInfo = JsonConvert.DeserializeObject<List<SeatsInfo>>(seatResponse.Content);

            //Further Implementation
        }
    }
}

Expected packet view in WireShark: enter image description here

Actual packet view in WireShark: enter image description here

sids are generated each time I successfully authorized to connect to the device, so they obviously differ.

The screenshot of my expected view is taken from the exact same block of codes in a plain console app. Everything works fine there. All the NuGet packages installed on the two programmes are of the same version, I even tried running them side-by-side but it did not help.

Debugging screenshots showed that the Parameters did, in fact, contain the Cookie header: enter image description here

The header just did not go through for some reason. Would love to hear some thoughts in regards to this issue, many thanks in advance!

Current solution, just add another AddCookie() method:

            seatAvailRequest.AddHeader("sid", sessionID.ToString());
            seatAvailRequest.AddHeader("Cookie","sid = " + sessionID.ToString());
            seatAvailRequest.AddCookie("sid", sessionID.ToString());

This somehow does not work with just AddCookie() or AddHeader(), so for now they are both going in. Hope this helps someone, and thank yall for reading!


Solution

  • So, in recap I was lacking the Cookie Header in the API call thus making the call being unsuccessful (the device did not authorized it).

    After messing around with the AddCookie() method, particularly:

        seatAvailRequest.AddCookie("sid", sessionID.ToString());
    

    The device gave me a different error - 404 Not Found, before was 401 Unauthorized: enter image description here

    Finally my dumb mind resorted using both the AddCookie() along with AddHeader() and voilà: enter image description here

    It works! Only after 5 hours of painful debugging and mind-numbing searches. Although the new packet is a bit larger, but I do not have to worry about that for now, or in the future realistically speaking.

    Thanks you all for just following along, still open for suggestions and improvements!