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.
CodePudding user response:
I'm pretty sure the content type you define using the AddParameter
is the culprit. Have you tested it like so:
request.AddParameter("application/json", datapoint, ParameterType.RequestBody);
or using AddJsonBody
request.AddJsonBody(datapoint);