Restsharp和有效载荷 [英] Restsharp and payload

查看:75
本文介绍了Restsharp和有效载荷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用Restsharp发送此消息:

请求方法:POST

内容类型:application / json

请求有效载荷:你好





我的代码存在的问题是收到的消息是你好而不是你好

如何删除符号?



thxs



我尝试过:



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);

推荐答案

您正在发送 JSON [ ^ ]请求。因此,请求est body必须是有效的JSON。



你好是有效的JSON; hello (不带引号)不是。



如果接收请求的代码期望字符串为如果没有引号发送,则它不接受JSON数据。您需要找出支持的内容,并使用适当的库提交请求。
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.


您好,



请尝试更改您的代码:



Hi,

Please try and change your code from:

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:





To:

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);





另一种选择是你的接收Web API /服务是使用Newtonsoft来de - 将输入参数串行化为字符串。



例如:





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

For example:

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


这篇关于Restsharp和有效载荷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆