1

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;
        }
2
  • 1
    In your defaultRequestHeader add content-type with value application/json Commented Dec 10, 2021 at 20:27
  • @Jawad, thank you so much! It helped Commented Dec 10, 2021 at 20:38

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.