How to send custom HTTP headers in C#
I use Ranorex in order to perform UI Tests, and there went the time to mimic some Postman queries to WebServices.
The Postman queries has been configured to add custom headers to some GET and POST ones.
This is one of the possible ways to to that.
First you need to install
System.Net.Http
Then, this is the code sample to do the trick is:
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
// Depending on your use case, you might need more
namespace RKTMB
{
public class Utils {
public static HttpClient client = new HttpClient();
public static async Task PostRequest()
{
string p = "";
string payload = "{\"CustomerId\": 5,\"CustomerName\": \"Pepsi\"}";
HttpContent c = new StringContent(payload,Encoding.UTF8, "text/plain" );
c.Headers.Add("X-Mihamina", "RKTMB-Value");
HttpResponseMessage response = await client.PostAsync("https://b0bd001850bfc022248470abd6db4.m.pipedream.net", c);
return p;
}
}
}