Click here to Skip to main content
15,888,202 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm using Restsharp to send this message :
Request Method:POST
Content-Type:application/json
Request payload : hello


The problem with my code is the message received is "hello" instead of hello
How to remove the symbols " ?

thxs

What I have tried:

client_ = new RestClient(url);
client_.Authenticator = new HttpBasicAuthenticator("admin", "admin");

RestRequest request = new RestRequest("tag/add", Method.POST);
request.AddHeader("Content-Type", "application/json");
request.RequestFormat = DataFormat.Json;
request.AddBody("hello");
client_.Execute(request);
Posted
Comments
Richard Deeming 16-Feb-16 10:51am    
Is that actually what's received, or just what you see in the debugger?
Member 12330910 16-Feb-16 11:05am    
What's I see what's received
Richard Deeming 16-Feb-16 11:10am    
So if you put a proxy like Fiddler[^] between the two systems, the request body contains "hello" rather than just hello?

Because if you're looking at it in the debugger, Visual Studio automatically displays the string within quotes, rather than showing you the raw value.

You're sending a JSON[^] request. Therefore, the request body has to be valid JSON.

"hello" is valid JSON; hello (without the quotes) is not.

If the code that's receiving the request is expecting the string to be sent without quotes, then it doesn't accept JSON data. You will need to find out what it does support, and use an appropriate library to submit the request.
 
Share this answer
 
Hi,

Please try and change your code from:

C#
client_ = new RestClient(url);
client_.Authenticator = new HttpBasicAuthenticator("admin", "admin");
 
RestRequest request = new RestRequest("tag/add", Method.POST);
request.AddHeader("Content-Type", "application/json");
request.RequestFormat = DataFormat.Json;
request.AddBody("hello");
client_.Execute(request);


To:

C#
var client_ = new RestClient(url);
client_.Authenticator = new HttpBasicAuthenticator("admin", "admin");

RestRequest request = new RestRequest("tag/add", Method.POST);
request.AddHeader("Content-Type", "text/plain");
request.AddBody("hello");
client_.Execute(request);


Another option is in your receiving Web API/Service is to use Newtonsoft to de-serialize the input parameter into a string.

For example:

C#
public string TagAddWebService(string bodyInput)
{
    string value = Newtonsoft.Json.JsonConvert.Deserailize<string>(bodyInput);
    // bodyInput = "Hello"
    // value = Hello
}</string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900