I've been integrating this api on a Worker Service https://apidocs.driv.in/#40fe495d-1323-43fe-8690-9ff89b098ab2 but i am having trouble with a RestSharp request.
By far, i've been trying the following code to send the information to Driv.In
CODE #1
var urlSend = https://external.driv.in/api/external/v2/clients;
var clientSend = new RestClient(new RestClientOptions(urlSend) { Timeout = 10000 });
var requestApi = new RestRequest();
requestApi.AddHeader("X-API-Key", API_KEY);
requestApi.AddHeader("Content-Type", "application/json");
var body = JsonConvert.SerializeObject(ordRequest, Formatting.Indented, serSettings);
requestApi.AddOrUpdateParameter("application/json", body, ParameterType.RequestBody);
var response = clientSend.ExecutePostAsync(requestApi).GetAwaiter().GetResult();
But i get the following error.
{"success":false,"response":{"description":"822: unexpected token at '--a5964b0b-10a0-4a33-a35b-7f8bc8b80d3a
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=\"application/json\"
{
\"clients\": [
{
\"code\": \"1111111111\",
\"name\": \"DEMO INC.\",
\"client_type\": \"customer\"
}
]
}
--a5964b0b-10a0-4a33-a35b-7f8bc8b80d3a--
'"},"details":"bad_request"}
The second option, i've been trying it's to use the AddJsonBody using the following code
CODE #2
var urlSend = https://external.driv.in/api/external/v2/clients;
var clientSend = new RestClient(new RestClientOptions(urlSend) { Timeout = 10000 });
var requestApi = new RestRequest();
requestApi.AddHeader("X-API-Key", API_KEY);
requestApi.AddHeader("Content-Type", "application/json");
requestApi.RequestFormat = DataFormat.Json;
requestApi.AddJsonBody(client);
var response = clientSend.ExecutePostAsync(requestApi).GetAwaiter().GetResult();
which produces a Success Response but there is a problem here. First, it doesn't ignore NULL values, The JsonIgnore Attribute is not working here and custom naming using JsonProperty it's also not applied. In conclusion, it works but it's not sending the right format.
BUT
There is also another problem.
With Code #1, logs are showing that the request is adding an extra parameter at the beggining with the name "_json" and duplicating the request string inside of them.
{
"_json": "{
\"clients\":
[
{
\"code\": \"1111111111\",
\"name\": \"DEMO INC.\",
\"client_type\": \"customer\"
}
]
}",
"client":
{
"_json": "{
\"clients\":
[
{
\"code\": \"1111111111\",
\"name\": \"DEMO INC..\",
\"client_type\": \"customer\"
}
]
}"
}
}
And with the CODE #2, works as expected in documentation but not using the name convention generated with the attributes.
{
"clients":
[
{
"code": "11111111111",
"name": "DEMO INC.",
"clientType": "customer",
"contactName": null,
"contactPhone": null,
"contactEmail": null
}
]
}
I kindly ask your help to solve this, i've been trying a lot of things before posting this question here but nothing seems to work.
This is the first time i'm having trouble with RestSharp, i used to send JsonBody using CODE #1 all my life but i never had an request like this with duplicity and extra parameters like "_json".
Thanks in advance!