Skip to content

Instantly share code, notes, and snippets.

@stefanhendriks
Last active May 11, 2016 18:43
Show Gist options
  • Save stefanhendriks/8ed40d617ce3a339af3e77e5dcd4561c to your computer and use it in GitHub Desktop.
Save stefanhendriks/8ed40d617ce3a339af3e77e5dcd4561c to your computer and use it in GitHub Desktop.
Helper class to construct a HttpRequest message for easily POSTing data (Asp.net Core)
public class PostRequestHelper
{
public static HttpRequestMessage Create(String path, Dictionary<string, string> formPostBodyData)
{
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, path)
{
Content = new FormUrlEncodedContent(ToFormPostData(formPostBodyData))
};
return httpRequestMessage;
}
public static List<KeyValuePair<string, string>> ToFormPostData(Dictionary<string, string> formPostBodyData)
{
List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
formPostBodyData.Keys.ToList().ForEach(key =>
{
result.Add(new KeyValuePair<string, string>(key, formPostBodyData[key]));
});
return result;
}
public static HttpRequestMessage CreateWithCookiesFromResponse(string path, Dictionary<string, string> formPostBodyData,
HttpResponseMessage response)
{
var httpRequestMessage = Create(path, formPostBodyData);
return CookiesHelper.CopyCookiesFromResponse(httpRequestMessage, response);
}
}
@stefanhendriks
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment