-1

I try to access to the REST API from NetExplorer. It works when I send a request with postman :

enter image description here

But It doesn't with my C# code :

var client = new RestClient("https://patrimoine-click.netexplorer.pro/api/auth");

        var ReqAuth = new { user = "xxxxxxxxxxxxxxxxxx", password = "xxxxxxxxxxxxx" };

        JsonResult result = new JsonResult(ReqAuth);
        
        var request = new RestRequest(result.ToString(), Method.Post);
        
        request.AddHeader("Accept", "application/json");
        
        RestResponse response = await client.ExecuteAsync(request);

Here's the error message :

{"error":"Il n'existe aucune m\u00e9thode de l'API pouvant r\u00e9pondre \u00e0 votre appel."}

In english, there's no API method to resolve your call

If somebody can help me ...

Thanks

  • 1
    I suggest you to use some kind of HTTP request monitoring tool, like fiddler, to see the difference between your two calls. – SᴇM Jul 18 '22 at 07:32
  • Use fiddler (or fiddler like app) to monitor the https call. But, can you try to set json body using standard RestSharp method like that: `request.RequestFormat = DataFormat.Json; request.AddJsonBody(new { user = "xxxxxx", password = "xxxxxxx" });`? JsonResult is not the best way to send a payload to an api using RestSharp! – ale91 Jul 18 '22 at 07:38
  • You can print the request details like headers and compare that with what happens in Postman. For printing http headers of Restsharp , see [this](https://stackoverflow.com/questions/15683858/restsharp-print-raw-request-and-response-headers) – Anand Sowmithiran Jul 18 '22 at 07:38
  • 1
    I don't think that this is how you add a JSON Body in RestSharp. See: https://restsharp.dev/usage.html#addstringbody – Fildor Jul 18 '22 at 07:41

1 Answers1

1

You are using the constructor of RestRequest wrong, the constructor does not take in the content (body) like that. Try using it with AddJsonBody like so:

var client = new RestClient("https://patrimoine-click.netexplorer.pro/api/auth");
var ReqAuth = new { user = "xxxxxxxxxxxxxxxxxx", password = "xxxxxxxxxxxxx" };
 
var request = new RestRequest();
request.Method = RestSharp.Method.Post;
request.AddJsonBody(ReqAuth);
    
request.AddHeader("Accept", "application/json");
    
RestResponse response = await client.ExecuteAsync(request);

Documentation: https://restsharp.dev/usage.html#request-body

Esko
  • 4,109
  • 2
  • 22
  • 37