0

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!

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Code 1 and Code 2 are identical? – stuartd Feb 17 '22 at 13:47
  • Thank you very much @stuartd, i have updated my question with the correct code. – Rafael Gonzales Feb 17 '22 at 13:53
  • it looks like in your first example that is multi-part - that's the delimiter. also don't use `.GetAwaiter().GetResult();` – Daniel A. White Feb 17 '22 at 13:55
  • Thanks for your answer @DanielA.White, what do you mean with Multi-part? By the way, i edited the CODE#1, accidentally copied the same code on both regions. – Rafael Gonzales Feb 17 '22 at 13:58
  • https://stackoverflow.com/questions/16958448/what-is-http-multipart-request – Daniel A. White Feb 17 '22 at 13:59
  • I still couldn't find a reason why there is a "_json" at the beginning of the request but downgrading the RestSharp version from 107.1.1 to 106.12.0 seems to work again with no "_json" at the beginning anymore. I still would like to find what happened with the newest version. – Rafael Gonzales Feb 20 '22 at 02:59

1 Answers1

0

Replace these lines:

requestApi.AddHeader("Content-Type", "application/json");
 
var body = JsonConvert.SerializeObject(ordRequest, Formatting.Indented, serSettings);
requestApi.AddOrUpdateParameter("application/json", body, ParameterType.RequestBody);

with

requestApi.AddJsonBody(ordRequest);

If you absolutely must use NewtonsoftJson and custom serialisation options, configure the serializer with client.UseNewtonsoftJson(options) and then again use AddJsonBody.

It's all described in the docs.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83