【发布时间】:2018-07-19 14:16:56
【问题描述】:
我需要将 API 集成到我的应用中。 文档说:
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 中创建此功能的文档:
public function testRld() {
$client = new GuzzleHttp\Client();
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 (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
但我收到了一条消息:
RequestException.php 第 111 行中的 ServerException:服务器错误:
POST http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise导致500 Internal Server Error响应:缺少参数: 用户名。
当我在 POSTMAN 应用程序上尝试这个时,一切都很好,我得到了响应:
<?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 请求工作正常...
【问题讨论】:
-
username!=Username。首先查看它是否区分大小写。 -
可能是案例问题?你写的是用户名,而不是用户名
-
您的用户名和密码以大写字母开头
-
我尝试了,但这不是问题......我再次收到相同的错误消息
-
按照the guzzle documentation 所说的方式尝试一下。
标签: php laravel api http-post postman