首页 > 解决方案 > 客户端发送的请求语法错误,当我使用Requests模拟邮递员请求时

问题描述

如何使用 Requests 模仿 Postman http 请求?

标头: 在此处输入图像描述

身体: 在此处输入图像描述

代码是:

标题:

Authorization: Bearer 123456
Content-Type: application/json

身体:

{
    "amount":"12",
    "currency":"USD",
    "reference":"20174647312238437828",
    "ipn_url":"http://website.com/ipn"
}

我使用上位邮递员请求成功。但是当我使用Requests来模仿邮递员的请求时,我得到了错误:

    $url = QRCODE_URL;

    $data = array(
        'amount'=> 12,
        'currency'=>'USD',
        'reference'=>'123456765432',
        'ipn_url'=>'http://website.com/ipn'
    );
    // $token
    $headers = array("Authorization"=>"Bearer 123456", "Content-Type"=>"application/json");
    $options = $data;

    $request = Requests::post($url, $headers, $data);

    echo '<pre>';
    print_r($request);

我收到这样的错误:

HTTP Status 400 - 
type Status report

message 

description The request sent by the client was syntactically incorrect.

标签: phphttphttprequest

解决方案


我看到您没有将数据编码为 JSON,所以我尝试了以下操作:

$url = "https://api.hantepay.com/v1.3/transactions/qrcode";

$data = array(
    'amount'=> 12,
    'currency'=>'USD',
    'reference'=>'123456765432',
    'ipn_url'=>'http://website.com/ipn'
);

$headers = array("Authorization"=>"Bearer 123456", "Content-Type"=>"application/json");
$options = $data;

$request = Requests::post($url, $headers, json_encode($data));

echo '<pre>';
print_r($request);

它工作正常:

Requests_Response Object
(
    [body] => {"amount":"12","code_url":"https://pay.usagms.com/api/v1.0/payment/partners/XHYCRN/orders/03500201812012144392663016/retail_pay","currency":"USD","id":"201812020238444097","reference":"123456765432","time":"2018-12-02 02:44:38","timeout":120}
    [raw] => HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 242
Date: Sun, 02 Dec 2018 02:44:39 GMT
Via: 1.1 google
Alt-Svc: clear
Connection: close

{"amount":"12","code_url":"https://pay.usagms.com/api/v1.0/payment/partners/XHYCRN/orders/03500201812012144392663016/retail_pay","currency":"USD","id":"201812020238444097","reference":"123456765432","time":"2018-12-02 02:44:38","timeout":120}
    [headers] => Requests_Response_Headers Object
        (
            [data:protected] => Array
                (
                    [server] => Array
                        (
                            [0] => Apache-Coyote/1.1
                        )

                    [content-length] => Array
                        (
                            [0] => 242
                        )

                    [date] => Array
                        (
                            [0] => Sun, 02 Dec 2018 02:44:39 GMT
                        )

                    [via] => Array
                        (
                            [0] => 1.1 google
                        )

                    [alt-svc] => Array
                        (
                            [0] => clear
                        )

                )

        )

    [status_code] => 200
    [protocol_version] => 1.1
    [success] => 1
    [redirects] => 0
    [url] => https://api.hantepay.com/v1.3/transactions/qrcode
    [history] => Array
        (
        )

    [cookies] => Requests_Cookie_Jar Object
        (
            [cookies:protected] => Array
                (
                )

        )

)

推荐阅读