簡體   English   中英

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

[英].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.
為什么會發生這種情況,我該如何解決?

     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;
        }

我正在使用 PostAsync 發送 JSON 請求:

        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;
        }

正如@Javad 在評論中提到的那樣,添加值為 application/json 的內容類型解決了這個問題。 就我而言,我必須將“application/json”作為參數傳遞給 PostAsync

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM