I need to integrate an API into my app. The docs say:
HTTP POST
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
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
and response is:
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>
Reading the docs I create this function in 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]);
}
}
but I got a message:
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.
When I try this at POSTMAN app everything is fine and there I get response like:
<?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>
Please help me to solve this problem? What is wrong with my code? Why I got the error, and POSTMAN request works fine ...
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',
]
]);
or
$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',
]
]);