【问题标题】:Calling PayU rest api (create order) returns html instead of json response调用 PayU rest api(创建订单)返回 html 而不是 json 响应
【发布时间】:2019-11-12 05:15:55
【问题描述】:

我正在尝试将订单发布到 PayU 支付网关,使用像 post man 这样的 Rest Client 工具,我也遇到了同样的问题。

我正在尝试使用C#发布,订单创建成功但响应不符合预期,应该是一个json对象,包含插入的订单id和重定向url,但当前是html响应!

C# 代码响应:

我使用 restsharp 库的 C# 代码:

 public IRestResponse<CreateOrderResponseDTO> CreateOrder(CreateOrderDTO orderToCreate)
    {

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        var actionUrl = "/api/v2_1/orders/";

        var client = new RestClient(_baseUrl);

        var request = new RestRequest(actionUrl, Method.POST)
        {
            RequestFormat = DataFormat.Json
        };

        request.AddJsonBody(orderToCreate);


        request.AddHeader("authorization", $"Bearer {_accessToken}");
        request.AddHeader("Content-Type", "application/json");

        var response = client.Execute<CreateOrderResponseDTO>(request);

        if (response.StatusCode == HttpStatusCode.OK)
        {
            return response;
        }

        throw new Exception("order not inserted check the data.");


    }

我使用内置 WebRequest 的 C# 代码也返回相同的 html:

 public string Test(string url, CreateOrderDTO order)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Accept = "application/json";

        httpWebRequest.Method = "POST";
        httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + _accessToken);

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {

            streamWriter.Write(new JavaScriptSerializer().Serialize(order));
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            return result;
        }
    }

谁能告诉我在这里错过了什么?

【问题讨论】:

  • 什么是 HTML?是错误页面吗?
  • @Amy 您好,该html包含一个巨大的svg文件,您可以找到html文件供您参考drive.google.com/…

标签: c# rest payumoney payu


【解决方案1】:

经过一些尝试,我发现 PayU rest api 返回 302(找到)也 ResponseUri 而不是 OK 200 如预期的那样。

默认情况下rest客户端会自动重定向到这个url,所以我收到了支付页面的html内容。

解决办法是:

client.FollowRedirects = false;

希望这对任何人都有用。

【讨论】:

  • 所以基本上他们发送带有 302 标头的正确 JSON 重定向到支付网站。如果重定向被禁用(在 HttpWebRequest request.AllowAutoRedirect = false; 的情况下),可以从响应中正常读取 JSON。我写这篇评论的唯一原因是,我无法从这个问题和答案(即使它在那里)或 API 文档(它也在那里)中得到它。如果我对此有疑问,也许其他人会发现此评论很有用。
【解决方案2】:

另外,我想补充一点,Mohammad 的上述回答是正确的,因为要获取我们需要将 AllowAutoRedirect 设置为 false 的响应 URL。我一直在尝试在控制台应用程序中实现 PayU LatAM WebCheckout,但我遇到了类似的问题。我从这里给出的答案中得到了一些启发:How to get Location header with WebClient after POST request

根据答案,我写了一个示例代码:

public class NoRedirectWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var temp = base.GetWebRequest(address) as HttpWebRequest;
        temp.AllowAutoRedirect = false;
        return temp;
    }
}

创建上述类后,我在我的Main方法中编写了以下代码:

var request = MakeRequestLocation(new NoRedirectWebClient());
var psi = new ProcessStartInfo("chrome.exe");

psi.Arguments = request.ResponseHeaders["Location"];
Process.Start(psi);

我现在在同一个类中调用函数 MakeRequestLocation。

private static WebClient MakeRequestLocation(WebClient webClient)
{
    var loginUrl = @"https://sandbox.checkout.payulatam.com/ppp-web-gateway-payu/";
    NameValueCollection formData = new NameValueCollection
    {
        {"ApiKey", "4Vj8eK4rloUd272L48hsrarnUA" },
        {"merchantId", "508029" },
        {"accountId", "512321" },
        {"description", "Test PAYU" },
        {"referenceCode", "SomeRandomReferenceCode" },
        {"amount", "2" },
        {"tax", "0" },
        {"taxReturnBase", "0" },
        {"currency", "USD" },
        {"signature", "Signature generated via MD5 sum" },
        {"buyerFullName", "test" },
        {"buyerEmail", "test@test.com" },
        {"responseUrl", @"http://www.test.com/response" },
        {"confirmationUrl",  @"http://www.test.com/confirmation" }
    };
    webClient.UploadValues(loginUrl, "POST", formData);

    return webClient;
}

上述函数返回的对象包含一个名为 location 的标头。 location 的值是您进行 webcheckout 所需的 URL。

【讨论】:

    【解决方案3】:

    在邮递员中,解决方案是关闭重定向,如下图所示:

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 2022-01-15
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2014-10-24
    • 2018-12-19
    相关资源
    最近更新 更多