Home > database >  Restsharp adds guid to body, how do I remove it?
Restsharp adds guid to body, how do I remove it?

Time:03-06

My code is below, it is very simple. I was trying to match a postman call and it came pretty close, but when I made the call in postman it looks different, specifically restsharp adds a guid around the body that I saw in fiddler. The guide seems to break my calls, how do I remove it?

 string url = "https://url/api/";

                var client = new RestClient(url);

                client.AddDefaultHeader("x-ads-dev", "Key");
                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);

And here is what Fiddler shows:

POST http://url/api/telemetry HTTP/1.1
Host: api.adsprism.com
x-ads-dev: key
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
User-Agent: RestSharp/107.3.0.0
Accept-Encoding: gzip, deflate, br
Content-Type: application/json; boundary="717cb9fd-684d-424e-bd0a-cf6b0bb11bac"
Content-Length: 398

--717cb9fd-684d-424e-bd0a-cf6b0bb11bac
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="application/json"

[{"entityID":2123,"locationID":33,"data":[{"dateTime":"2020-08-03 23:05:00","reading":0,"flags":0,"quality":15,"ignore":false},{"dateTime":"2020-09-27 03:10:00","reading":0,"flags":0,"quality":15,"ignore":false}]}]
--717cb9fd-684d-424e-bd0a-cf6b0bb11bac--

The --717cb9fd-684d-424e-bd0a-cf6b0bb11bac-- guid is not there in the fiddler postman request and it seems to be causing a bad request error from restsharp. So how do I make restsharp not generate it?

EDIT: Added postman fiddler to show the difference.

POST http://api.url/api/telemetry HTTP/1.1
x-ads-dev: key
Content-Type: application/json
User-Agent: PostmanRuntime/7.29.0
Accept: */*
Postman-Token: 5f186e6d-cad0-482c-b7b8-a50559f24b87
Host: api.adsprism.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 513

[
    {
        "entityID": 2123,
        "locationID": 33,
        "data": [
            {
                "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
            }
        ]
    }
]

CodePudding user response:

Reading the docs can help.

const string url = "https://url/api/";

var client = new RestClient(url);
client.AddDefaultHeader("x-ads-dev", "Key");

var request = new RestRequest("telemetry", Method.Post);
request.AddStringBody(datapoint, DataType.Json);
var response =  await client.PostAsync(request);
  • Related