How to make a HTTP POSTrequest to an API using headers

假装没事ソ 提交于 2019-12-24 10:51:13

问题


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 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>

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


回答1:


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',
    ]
]);



回答2:


Use form_params:

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

The PSR-7 standard specifies interfaces for HTTP messages, including requests and responses. If you would like to obtain an instance of a PSR-7 request instead of a Laravel request, you will first need to install a few libraries. Laravel uses the Symfony HTTP Message Bridge component to convert typical Laravel requests and responses into PSR-7 compatible implementations:

Install the necessary dependencies as well:

composer require symfony/psr-http-message-bridge
composer require zendframework/zend-diactoros


来源:https://stackoverflow.com/questions/48695178/how-to-make-a-http-postrequest-to-an-api-using-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!