user4796879
user4796879

Reputation:

php - CURL - Getting HTTP code if/else to test

I want to do 404 testing on an API using curl and php

While valid requests to the token generation endpoint (/api/Utilities/StashData) will receive an HTTP 200 response containing a token for use with Leap, invalid requests will receive an HTTP 400 response (Bad Request). If your system receives an HTTP 400 response from the API, it must not attempt to proceed to Leap. The content of this response will contain a JSON-serialized object describing the validation error(s), as per the following example:

[

       {

              "Key": "CapeConsumers.TrackerNumber",

              "Errors": [

                     {

                           "ErrorCode": "CC002",

                           "Description": "The value is required."

                     }

              ]

       }

]

As you can see, the response contains an array of objects, where each object has a Key property which refers to the key submitted to the service, and an Errors property describing the reason(s) the value was rejected. The ErrorCode values are predefined and guaranteed never to change, the Description is dynamic for certain errors and exists mainly to aid debugging

The codes

CC001

Invalid value.

The value does not conform to one or more validation rules defined for the key.

CC002

The value is required.

A required value has been omitted from the request. Sending null or a blank string for a required value will also result in this error (unless a blank string has been defined as valid by supporting documentation).

CC003

The value is not of the correct type. Expected {0}, found {1}.

The system expected the value to be of one type, but a different type was submitted. (e.g. a number was sent instead of a string)

CC004

The origin is not valid for the specified campaign.

Campaigns are linked to specific integrating parties. If an integrating party uses a tracker number that isn’t assigned to it, this error will be returned.

CC005

The campaign is not currently active.

The tracker number references a campaign that either hasn’t started or has already come to an end.

Here is the code I have tried.

$endpointUser = "myuser";
//
$endpointPassword = "mypass";
//
$url = "https://webservices_test.capeconsumers.co.za/api/Utilities/StashData";

$iframeUrl =  "https://onlineapplicationtest.capeconsumers.co.za/Bridge/CapeConsumersSACommercial?token=";

$fields = array(
    "User.IdentityNumber"=> $_GET['security_phrase'],
    "CapeConsumers.TrackerNumber"=> "6E273247DB4840G3",
    "Call.AgentReference"=> $_GET['user'] ,
    "Call.RecordingReference"=> $_GET['security_phrase']
);

$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERPWD, $endpointUser . ":" . $endpointPassword);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$token = curl_exec($process);
curl_close($process);
$token = str_replace('"','',$token);

$httpCode = curl_getinfo($process, CURLINFO_HTTP_CODE);


if($httpCode == 400) {

 echo "there was an error"; exit;

}

?>


<iframe src="<?php echo $iframeUrl . $token; ?>" width="100%" height="800"></iframe>

My problem is that if I deliberately give it a bad response, it will still generate the iframe instead of exiting at the if statement for the 400 error.

I am unsure of my logic as this is my first attempt at curl

Upvotes: 1

Views: 1633

Answers (2)

Rene Korss
Rene Korss

Reputation: 5484

You are getting response code too late.

curl_close manual says:

Closes a cURL session and frees all resources. The cURL handle is also deleted.

Order must be:

$httpCode = curl_getinfo($process, CURLINFO_HTTP_CODE);
curl_close($process);

Otherwise there is no CURL handle anymore and therefore you can't retrieve response code.

Upvotes: 0

Ravi Sinha
Ravi Sinha

Reputation: 39

404 will not be returned by API, it would be thrown if and only if requested url doesn't exist or running. To generate 404 deliberately, pls modify the hostsection of url, so that it refers to wrong location and thus should result 404. Hope this helps.

Upvotes: 0

Related Questions