Tuesday, 20 August 2013

What is the correct way to make a POST request to a Rest api with an authentication token?

What is the correct way to make a POST request to a Rest api with an
authentication token?

I need to consume the api documented here:
[http://docs.opsview.com/doku.php?id=developer:restapi][1]
The login call returns a token successfully. However, when I try other
calls, I'm getting the following error:
failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
This is the function I wrote to make my calls:
public function sendRequestGetResults($path, $data = [], $method = 'GET',
$headers = [], $contentType = "application/x-www-form-urlencoded")
{
return json_decode
(
file_get_contents
(
'https://' . $this->apiHost . '/rest/' . $path,
false,
stream_context_create
(
[
'http' =>
[
'header' => array_merge
(
[
"Content-type: " . $contentType
],
$headers
),
'method' => $method,
'content' => http_build_query
(
$data
)
],
]
)
)
);
}
I tried adding the login and the token returned by the login call to the
headers of the request by calling my function with the a $headers
parameter as follows, but still no luck:
[
"X-Opsview-Username: myUsername",
"X-Opsview-Token: myToken"
]
I also tried modifying the function to concatenate the string of headers
instead of using an array, but got the same result:
What might I be missing?

No comments:

Post a Comment