尝试让restsharp匹配来自邮递员的curl代码
这个问题已经被问过一百次了,但似乎没有一个对我有用。我有以下curl代码:
curl --location --request POST 'https://endpoint/telemetry' \
--header 'x-ads-dev: akeyvalue' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"entityID": 2123,
"locationID": 33,
"dataPoints": [
{
"dateTime": "2020-08-03 23:05:00",
"reading": 0,
"flags": 0,
"quality": 0,
"ignore": true
},
{
"dateTime": "2020-09-27 03:10:00",
"reading": 0,
"flags": 0,
"quality": 0,
"ignore": true
}
]
}
]'
当我在邮递员中运行它时,我从服务器得到的响应与我尝试将其工作到restsharp时不同。这是我的rest Sharp代码:
try {
string url = "https://endpoint/";
var client = new RestClient(url);
client.AddDefaultHeader("x-ads-dev", "akeyvalue");
var request = new RestRequest("telemetry", Method.Post);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", datapoint, ParameterType.RequestBody);
var response = await client.PostAsync(request);
变量数据点的值是:
[
{
"entityID":2123,
"locationID":33,
"dataPoints":[
{
"dateTime":"2020-08-03 23:05:00",
"reading":0,
"flags":0,
"quality":0,
"ignore":true
},
{
"dateTime":"2020-09-27 03:10:00",
"reading":0,
"flags":0,
"quality":0,
"ignore":true
}
]
}
]
当我使用curl代码通过postman运行它时,服务器接受请求,但是当我通过restsharp运行它时,我得到了错误的请求错误。
编辑代码以匹配以下评论,因为这样做并没有解决我的问题。
This has been asked a hundred times, but none of them seem to work for me. I have the following curl code:
curl --location --request POST 'https://endpoint/telemetry' \
--header 'x-ads-dev: akeyvalue' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"entityID": 2123,
"locationID": 33,
"dataPoints": [
{
"dateTime": "2020-08-03 23:05:00",
"reading": 0,
"flags": 0,
"quality": 0,
"ignore": true
},
{
"dateTime": "2020-09-27 03:10:00",
"reading": 0,
"flags": 0,
"quality": 0,
"ignore": true
}
]
}
]'
When I run this in postman I get a different response from the server than when I try to work it into restsharp. Here is my rest sharp code:
try {
string url = "https://endpoint/";
var client = new RestClient(url);
client.AddDefaultHeader("x-ads-dev", "akeyvalue");
var request = new RestRequest("telemetry", Method.Post);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", datapoint, ParameterType.RequestBody);
var response = await client.PostAsync(request);
The value of the variable datapoint is:
[
{
"entityID":2123,
"locationID":33,
"dataPoints":[
{
"dateTime":"2020-08-03 23:05:00",
"reading":0,
"flags":0,
"quality":0,
"ignore":true
},
{
"dateTime":"2020-09-27 03:10:00",
"reading":0,
"flags":0,
"quality":0,
"ignore":true
}
]
}
]
When I run it through postman with the curl code the server accepts the request, but when I run it through restsharp I get bad request error.
EDITED the code to match the below comment, as doing so didn't solve my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我非常确定您使用
AddParameter
定义的内容类型是罪魁祸首。您是否像这样测试过:或使用
AddJsonBody
I'm pretty sure the content type you define using the
AddParameter
is the culprit. Have you tested it like so:or using
AddJsonBody