0

Client

  public BaseResponseDTO<InvoiceResponseDTO> GetInvoiceInfo(InvoiceRequestDTO model)
    {
        _client.BaseUrl = new System.Uri(_serviceUrl + "/Invoice/GetInvoiceInfo");
        _request.Method = RestSharp.Method.POST;
        _request.AlwaysMultipartFormData = true;
        _request.UseDefaultCredentials = true;

        _request.AddParameter("SupplierCode", model.SupplierCode);
        _request.AddParameter("CompanyCode", model.CompanyCode);
        _request.AddParameter("Creator", model.Creator);
        _request.AddParameter("Type", model.Type);           

        IRestResponse response = _client.Execute(_request);
        var result = JsonConvert.DeserializeObject<BaseResponseDTO<InvoiceResponseDTO>>(response.Content);

        return result;
    }

And here is my API code

 [Route("GetInvoiceInfo")]
        [HttpPost]
        public GenericResponse Invoice([FromBody] SapInvoiceRequestModel model)
        {
            GenericResponse result = new GenericResponse();
            try
            {
                ZMC_TD_BILLINFO billinfo = new ZMC_TD_BILLINFO();
                var response = billinfo.CallZMC_TD_BILLINFO(new ZMC_TD_BILLINFO1
                {
                    GV_BUKRS = model.CompanyCode,
                    GV_LIFNR= model.SupplierCode,
                    GV_USNAM = model.Creator,
                    GV_TYPE = model.Type,                   
                    T_HEADER = new ZBILLHEADERINFO[0]
                });
                result.Data = response;
                result.IsSuccess = true;
                result.Message = "";
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.IsSuccess = false;
            }
            return result;
        }

'''I'm trying to call an API using the RestSharp library. After Execute method my web api request model is null. Am I missing something here? Thanks'''

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question? [Post parameter is always null](https://stackoverflow.com/questions/10984040/post-parameter-is-always-null) –  Sep 30 '22 at 13:56
  • this is not the answer i was looking for. – Emre Oztoprak Sep 30 '22 at 14:13
  • The response InvoiceResponseDTO is not matching your model. The request may be wrong. First thing to do is check the status of the response to see if you are getting 200 OK or an error status. An error means the server didn't accept the request. If you are getting 200 ok that response is in the body of the message and you have to data. Best thing to do is put break point after following line : IRestResponse response = _client.Execute(_request); – jdweng Sep 30 '22 at 14:23
  • "_this is not the answer i was looking for._" Then what answer are you looking for? Have you tried what has been suggested in the answer(s) of the question? Did it change anything whatsoever in what is being passed to the Invoice method (even if it were not the thing you would expect to be passed to the Invoice method)? –  Sep 30 '22 at 14:59
  • @MySkullCaveIsADarkPlace Yes I tried the accepted answers but it didn't work. This is the request model of my client : https://imgur.com/a/MbJsGv4 this is the api : https://imgur.com/a/MbJsGv4 – Emre Oztoprak Sep 30 '22 at 16:18
  • @jdwend, I got status code OK. – Emre Oztoprak Sep 30 '22 at 16:20
  • Hello @EmreOztoprak, Did you able to resolve the issue? Is it wokring now? Feel free to share, if you still have any concern. – Md Farid Uddin Kiron Oct 07 '22 at 06:42
  • Hello @MdFaridUddinKiron, Yes I resolved the issue. The problem is the parameter is should be application/json. Thank you for your interest – Emre Oztoprak Oct 09 '22 at 14:31

1 Answers1

0

You can try this to add json to the request body:

var client = new RestClient("http://www.example.com/Invoice/GetInvoiceInfo");
var request = new RestRequest();
request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", modelBody, ParameterType.RequestBody);
var response = client.Execute(request);
Sang Tran
  • 54
  • 7
  • ı tried but still post parameters are null . – Emre Oztoprak Oct 03 '22 at 11:56
  • Did you try to call API with Postman or Insomnia to test API work fine? – Sang Tran Oct 04 '22 at 03:15
  • Yes it is working with Postman. I don't know where could be the problem? – Emre Oztoprak Oct 04 '22 at 06:04
  • You can set a breakpoint in the line which before `client.Execute(request)`. So you can read all of property like `Method`, `Header`, `Parameter` to check if it is right or wrong – Sang Tran Oct 04 '22 at 07:01
  • Thanks for your help. https://imgur.com/a/JsdjeZX. where could be the problem? – Emre Oztoprak Oct 04 '22 at 14:03
  • Did you use `request.AddHeader("Accept", "application/json");` and `request.AddParameter("application/json", modelBody, ParameterType.RequestBody);` like I suggest. Because when I open your image, I see an property `Content-Type = application/x-www-form-urlencoded`. It have to be `application/json` because data you send with request is in `JSON` format – Sang Tran Oct 05 '22 at 01:19
  • "In responses, a Content-Type header provides the client with the actual content type of the returned content" "In requests, (such as POST or PUT), the client tells the server what type of data is actually sent." (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) – Sang Tran Oct 05 '22 at 01:22
  • Thanks for your help. I used equest.AddParameter("application/json", modelBody, ParameterType.RequestBody). and then the request parameters not null. – Emre Oztoprak Oct 05 '22 at 13:26