如何使用标头向 API 发出 HTTP POST 请求

2022-01-22 00:00:00 api http-post postman php laravel

我需要将 API 集成到我的应用中.文档说:

I need to integrate an API into my app. The docs say:

HTTP POST以下是一个示例 HTTP POST 请求和响应.显示的占位符需要替换为实际值.

POST /partnerhubwebservice.asmx/Authorise HTTP/1.1
Host: stage.example.co.uk
Content-Type: application/x-www-form-urlencoded
Content-Length: length

username=string&password=string

响应是:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<AuthorisationResult xmlns="http://webservice.example.co.uk/">
  <Token>string</Token>
</AuthorisationResult>

阅读我在 Laravel Controller 中创建此功能的文档:

Reading the docs I create this function in Laravel Controller:

public function testRld() {

    $client = new GuzzleHttpClient();
try {
    $res = $client->post('http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
            ],
    'x-www-form-urlencoded' => [
        'Username' => 'MMM_bookings',
        'Password' => 'passwordaaaa',
]
            ]);

return $res;

}

catch (GuzzleHttpExceptionClientException $e) {
        $response = $e->getResponse();
        $result =  json_decode($response->getBody()->getContents());

    return response()->json(['data' => $result]);

    }

}

但我收到了一条消息:

RequestException.php 第 111 行中的 ServerException:服务器错误:POSThttp://stage.example.co.uk/partnerhubwebservice.asmx/Authorise导致 500 Internal Server Error 响应:缺少参数:用户名.

ServerException in RequestException.php line 111: Server error: POST http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise resulted in a 500 Internal Server Error response: Missing parameter: username.

当我在 POSTMAN 应用程序上尝试这个时,一切都很好,我得到了响应:

When I try this at POSTMAN app everything is fine and there I get response ike:

<?xml version="1.0" encoding="utf-8"?>
<AuthorisationResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://webservice.example.co.uk/">
    <RequestSuccessful>true</RequestSuccessful>
    <ErrorMessage />
    <Token>27d67d31-999-44e0-9851-d6f427fd2181</Token>
</AuthorisationResult>

请帮我解决这个问题?我的代码有什么问题?为什么我收到错误消息,并且 POSTMAN 请求工作正常...

Please help me to solve this problem? What is wrong with my code? WHy I got the error , and POSTMAN request works fine ...

推荐答案

尝试在正文中以 bodyjson 形式发送用户名和密码,而不是 >application/x-www-form-urlencoded,像这样:

Try sending username and password inside of a body as body or json, instead of as application/x-www-form-urlencoded, like so:

$res = $client->post('http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
    'json' => [
        'username' => 'MMM_bookings',
        'password' => 'passwordaaaa',
    ]
]);

$res = $client->post('http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
    'body' => [
        'username' => 'MMM_bookings',
        'password' => 'passwordaaaa',
    ]
]);

相关文章