TimNguyenBSM
TimNguyenBSM

Reputation: 827

CURL POST Request with Cookie Authorization

I am trying to POST according to the following instructions:

Resource Information

HTTP Method: POST

Content-type: application/json

Accept: application/json

Authentication

Note: Either a Cookie or Authorization header is required.

Cookie: SESSION_ID=108259418ff689fc

Authorization: Basic WVdECUMScUZGGGBGOlhVODdGNXFkR253SA==

Example Request

POST https://sandbox.domain.com/api/authenticationTokens

Cookie: SESSION_ID=cdf5b882667d24a2

What is meant by "Either a Cookie or Authorization header is required." I don't understand how to read/interpret and implement the "Example Request".

Here is what I have so far:

    $parameters = array();

    $curl = curl_init();
    $header = array( "Accept: application/json", "Cookie: _SESSION_ID=cdf5b882667d24a2" );
    curl_setopt( $curl, CURLOPT_URL, "https://sandbox.domain.com/api/authenticationTokens" );
    curl_setopt( $curl, CURLOPT_POST, count( $parameters ) );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, http_build_query( $parameters ) );
    curl_setopt( $curl, CURLOPT_HTTPHEADER, $header );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
    $response = curl_exec( $curl );
    curl_close( $curl );

Upvotes: 2

Views: 3042

Answers (1)

Mike
Mike

Reputation: 24383

The example above is using cookie authentication. Cookies are sent in the request headers. To use cookie authentication you need to somehow obtain the session ID and substitute that into the code. I am guessing you need to somehow open a session previously and then use that value in the cookie, however that does seem like an odd way to authenticate for an API. Also, I'm not sure if the leading underscore is a typo or not. You might want to try it without the underscore if it doesn't work:

"Cookie: SESSION_ID=cdf5b882667d24a2"

Or, if you want to use HTTP authentication, you need to provide a username and password doing something like

curl_setopt( $curl, CURLOPT_USERPWD, 'username:password' );

If all you have is a single value, it would probably go in the password part leaving the username blank:

curl_setopt( $curl, CURLOPT_USERPWD, ':WVdECUMScUZGGGBGOlhVODdGNXFkR253SA==' );

Upvotes: 1

Related Questions