Search code examples
c#.net.net-core

Posting data from dotnetcore API to .net API not working


We are working in a DDD (Domain-Driven-Design) environment and we have dozens of API that is made with .Net. The latest site we are creating is a dotnetcore site which we want to be calling all the old .Net API:s. The problem is that i can't get any values to the old API. It becomes null every time i post something to the old API. When i create a dotnetcore api it workes fine. Is there a known problem for dotnetcore to call an old .net API?

OLD API:

          [HttpPost]
    [Route("GetIllustration")]
    public async Task<HttpResponseMessage> GetIllustration([FromBody] JsData illustrationRequest)
    {
        if(illustrationRequest == null)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request was null");
        }
        //var modelService = modelLookupService.Get(illustrationRequest.ModelName);
        //var illustration = modelService.Get(illustrationRequest);
        return Request.CreateResponse(HttpStatusCode.Accepted, illustrationRequest);
    }

Working dotnetcore:

          [HttpPost]
    [Route("GetIllustration")]
    public IActionResult GetIllustration([FromBody] JsData illustrationRequest)
    {
        if (illustrationRequest == null)
        {
            return BadRequest(illustrationRequest);
        }
        return Ok(illustrationRequest);
    }

The return type is different but otherwise it is the same method in different types of API.

EDIT 1:

Call to .net API:

 public IActionResult Get(GetIllustrationRequest request)
      {
        var req = new JsData()
        {
            CountryCode = "DE",
            DrawSideInfo = true,
            IlXml = "XML",
            ImageSizeHeight = 123,
            ImageSizeWidth = 1234,
            LargeImageSizeHeight = 12345,
            LargeImageSizeWidth = 123456,
            ModelName = "SUPERIOR",
            ShowInside = true,
            SupplierName = "NORMSTAHL",
            TopItemName = "SUPERIOR+42",
            XposText = "HAJ",
            YposText = "DÅÅ"
        };
        return PostAsJson("/GetIllustration", req);
    }

enter image description here

Working call to dotnetcore api:

   public IActionResult Get(GetIllustrationRequest request)
    {
        var req = new JsData()
        {
            CountryCode = "DE",
            DrawSideInfo = true,
            IlXml = "XML",
            ImageSizeHeight = 123,
            ImageSizeWidth = 1234,
            LargeImageSizeHeight = 12345,
            LargeImageSizeWidth = 123456,
            ModelName = "SUPERIOR",
            ShowInside = true,
            SupplierName = "NORMSTAHL",
            TopItemName = "SUPERIOR+42",
            XposText = "HAJ",
            YposText = "DÅÅ"
        };
        return PostAsJson("api/Configuration/GetIllustration", req);
    }

enter image description here

Edit 2:

Here is the actual post code:

   using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(BaseUri);
                var cancellationToken = new CancellationToken();
                client.DefaultRequestHeaders.Add("X-Content-Type-Options", "nosniff");                 
                if (!string.IsNullOrEmpty(OAuth2Token))
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                        "BEARER",
                        OAuth2Token);
                }
                var response = client.PostAsJsonAsync(path, value, cancellationToken).Result;
                var stringResult = response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<object>(stringResult.Result);
                return Ok(result);
            }

Solution

  • Apparently dotnetcore can't handle postasjsonasync method by some reason. So i had to do it somewhat manually changeing my method to this:

                    var content = JsonConvert.SerializeObject(value);
                    HttpContent httpContent = new StringContent(content, Encoding.UTF8, "application/json");
                    var response = client.PutAsync(path , httpContent, cancellationToken).Result;
                    var stringResult = response.Content.ReadAsStringAsync();
    

    instead of:

                var response = client.PutAsJsonAsync(path, value, cancellationToken).Result;
                var stringResult = response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<object>(stringResult.Result);