Need to build URL encoded query from model object for HttpClient
My Model is
class SaveProfileRequest
{
public string gName { get; set; }
public string gEmail { get; set; }
public long gContact { get; set; }
public string gCompany { get; set; }
public string gDeviceID { get; set; }
public string Organization { get; set; }
public string profileImage { get; set; }
public string documentImagefront { get; set; }
public string documentImageback { get; set; }
}
SaveProfileRequest request = new SaveProfileRequest() { gName = name, gEmail = email, gContact = phonenumber, gCompany = company,
gDeviceID = deviceId, Organization = "", profileImage = "", documentImageback = "", documentImagefront = "" };
string response = await RequestProvider.PostAsync<string, SaveProfileRequest>(uri, request);
I have a working code for content type JSON
content = new StringContent(JsonConvert.SerializeObject(data));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
Where data is of type TInput
Tried
content = new StringContent(System.Net.WebUtility.UrlEncode(JsonConvert.SerializeObject(data)));
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
but didn't workout
This can be done effortlessly with Flurl (disclaimer: I'm the author) as this exact scenario - parsing a .NET object's properties into URL-encoded key-value pairs - is supported right out the box:
Flurl uses
HttpClient
under the hood, so in the example above,resp
is an instance ofHttpResponseMessage
, just as if the call was made usingHttpClient
directly. You could also append.ReceiveString()
to the call if you just want the response body, or.ReceiveJson<T>()
if you're expecting a JSON response and want a matching .NET object of typeT
.JsonConvert
produces only json content. For urlencoded query you should construct instance ofFormUrlEncodedContent
. As constructor parameter it takes collection ofKeyValuePair<string, string>
. So the main trick is to build this collection for model object.You could use reflection for this purpose. But there is a simpler solution based on Json.net. It was described here and following
ToKeyValue()
extension method is a copy/paste from that blog post:Now you could build the url-encoded content as easy as:
You can use this simplified version