onlyo
onlyo

Reputation: 53

.NET Rest API Returns Response in XML format instead of JSON

I'm sending a request in JSON format to an API, but the response comes back (content variable) in XML format (Content-type=XML) instead of JSON.
Why it's happening and how can I fix that?

     public async Task<TransactionResponse> Capture(GatewayTransaction request)
        {

            var captureTransaction = PayURequestMapper.GetCapturePayload(request, this.gateway);

            HttpContent httpContent = new StringContent(captureTransaction, Encoding.UTF8, "application/json");
            var response = await this.restClient.PostAsync(
                this.gateway?.TargetURL,
                httpContent, true);

            var content = response.Content.ReadAsStringAsync().Result;
          
            return transactionResponse;
        }

I'm sending JSON request with PostAsync:

        public async Task<HttpResponseMessage> PostAsync(string url, HttpContent content, bool acceptHeader = false, string headerType = null)
        {
            HttpResponseMessage responseMessage;
            if (acceptHeader)
            {
                this.httpClient.DefaultRequestHeaders.Add("Accept", headerType);
            }

            using (content)
            {
                responseMessage = await this.httpClient.PostAsync(url, content);
            }

            return responseMessage;
        }

Upvotes: 1

Views: 1591

Answers (1)

onlyo
onlyo

Reputation: 53

As @Javad mentioned in the comments, adding content-type with value application/json solved the problem. In my case, I had to pass "application/json" to PostAsync as a parameter

Upvotes: 1

Related Questions