在发送 Rest 请求正文之前如何转义字符串?

How can i escape a string before sending in the body of a Rest request?

我有一个休息请求。正文如下:

 RestClient client = new RestClient(uri);
 RestRequest request = new RestRequest("", Method.POST);
 request.AddHeader("Authorization", $"Bearer {myToken}");
 request.AddHeader("Content-type", "application/json");
 string json = "{'body': {'contentType': 'html','content': '" + message + "'}}";                    
 request.AddJsonBody(json);
 IRestResponse response = client.Execute(request);

邮件中包含撇号,就像 John 的一样,因此 json 将是

       string json = "{'body': {'contentType': 'html','content': 'John's'}}";                    

这给出了一个错误,Bad Request。 我如何适应消息中的这个和任何其他特殊字符

您必须将 ' 替换为 \ "

string json= "{\"body\": {\"contentType\": \"html\",\"content\": \""+message+"\"}}";