I am sending data to a site, but when I add true
to the form content, it adds it as a string
. How do I convert it to a Boolean
type?
The result I want:
My actual result:
HttpClient client = new HttpClient();
StringContent content = new StringContent("{\"password\":\"123123asd\"," + "\"remember_me\":\""+true+"\"," + "\"username\":\"" +"feridisgenderli"+ "\"}");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.PostAsync(LoginAPI.ToString(), content);
var stringContent = await response.Content.ReadAsStringAsync();
Console.Write(stringContent);
You're specifically wrapping it in quotes:
StringContent content = new StringContent("{\"password\":\"123123asd\"," + "\"remember_me\":\""+true+"\"," + "\"username\":\"" +"feridisgenderli"+ "\"}");
Dont do that and it wont have quotes:
StringContent content = new StringContent("{\"password\":\"123123asd\"," + "\"remember_me\":true," + "\"username\":\"" +"feridisgenderli"+ "\"}");
But more generally, you usually wouldn't build up JSON using a string. Use an object and serialize it to JSON format.
You are concatenating a string, first of all. This happens to be okay in C# with boolean types, but not all languages will transform true
into "true"
. Consider serializing an object instead .
The c# code "\"remember_me\":\""+true+"\","
will result in "remember_me": "true",
because you have included \"
around your boolean value, ie:
Console.Write("\"" + true + "\""); // -> "true"
Console.Write("" + true); // -> true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.