Skip to content

Instantly share code, notes, and snippets.

@ander94lakx
Created May 6, 2020 09:38
Show Gist options
  • Save ander94lakx/4040eba79e29a2fbbd846e9273fe0fca to your computer and use it in GitHub Desktop.
Save ander94lakx/4040eba79e29a2fbbd846e9273fe0fca to your computer and use it in GitHub Desktop.
Modified version of CallApi() Method from swagger-codegen generated C# client that allows using Cookie headers
/// <summary>
/// Makes the HTTP request (Sync).
/// </summary>
/// <param name="path">URL path.</param>
/// <param name="method">HTTP method.</param>
/// <param name="queryParams">Query parameters.</param>
/// <param name="postBody">HTTP body (POST request).</param>
/// <param name="headerParams">Header parameters.</param>
/// <param name="formParams">Form parameters.</param>
/// <param name="fileParams">File parameters.</param>
/// <param name="pathParams">Path parameters.</param>
/// <param name="contentType">Content Type of the request</param>
/// <returns>Object</returns>
public Object CallApi(
String path, RestSharp.Method method, List<KeyValuePair<String, String>> queryParams, Object postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType, Dictionary<String, String> cookieParams = null)
{
var request = PrepareRequest(
path, method, queryParams, postBody, headerParams, formParams, fileParams,
pathParams, contentType);
// set timeout
RestClient.Timeout = Configuration.Timeout;
// set user agent
RestClient.UserAgent = Configuration.UserAgent;
// set cookies
var cookies = new CookieContainer();
foreach (var c in headerParams.Where(x => x.Key.Equals("Cookie", StringComparison.CurrentCultureIgnoreCase)))
{
var coso = c.Value.Split(new string[] { "; " }, StringSplitOptions.None);
foreach (string cookiestr in coso)
{
var k = cookiestr.Split('=');
if (k.Length < 2) continue;
if (k[1].Substring(k[1].Length - 1) == ";")
{
k[1] = k[1].Substring(0, k[1].Length - 1);
}
cookies.Add(new Uri(RestClient.BaseUrl.AbsoluteUri), new Cookie(k[0], k[1]));
}
}
RestClient.CookieContainer = cookies;
InterceptRequest(request);
var response = RestClient.Execute(request);
InterceptResponse(request, response);
return (Object) response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment