0

Hi I am trying to make JSON Request to a API but I am not sure how to as I have never worked on something similar ever before. I would really appreciate if someone can help me please.

Below is the request that I need to make:

request payload:

{
    "sessionId": "1234567890",
    "availabilityRequest": {
        "checkInDate": "29042014",
        "checkOutDate": "",
        "noRooms": 1,
        "noNights": 1,
        "userType": "leisure",
        "rateType": "standard",
        "roomPreference": [
            {
                "noAdult": 1,
                "noChild": 0
            }
        ],
        "siteCode": [
            "GB0758",
            "GB0746",
            "GB0738",
            "GB0755",
            "GB0742"
        ],
        "includeDisabled": "F"
    }
}

This is what I have done but I am getting error Array ( [error] => Array ( [code] => 4007 [message] => Invalid JSON POST data (unable to decode): ) )

       $postData = '{
                "sessionId":"1234567890",
                "availabilityRequest":
                {
                    "checkInDate": "29042014",
                    "checkOutDate": "",
                    "noRooms": 1,
                    "noNights": 1,
                    "userType": "leisure",
                    "rateType": "standard",
                    "roomPreference": 
                        [
                            { "noAdult":1, "noChild":0 }
                        ],
                    "siteCode": 
                        [
                        "GB0758","GB0746","GB0738","GB0755","GB0742"
                        ],
                    "includeDisabled":"F"
                }
            }';

        $ch = curl_init($url);
        curl_setopt_array($ch, array(
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_POSTFIELDS => $postData));

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // Send the request
        $response = curl_exec($ch);

        // Check for errors
        if($response === FALSE){
            die(curl_error($ch));
        }

        // Decode the response
        $responseData = json_decode($response, TRUE);

        // Print the date from the response
        print_r($responseData);

I would be really grateful is someone can help me please. Thank you

2
  • What does your $postData looks like. Commented Jul 3, 2015 at 10:20
  • I have edited the post which shows what my $postData looks like Commented Jul 3, 2015 at 11:13

2 Answers 2

1

Your JSON is invalid. Validate it using a tool such as http://jsonlint.com/

You should add an other } to the end to close it. Then you will have a valid JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

Json is valid only in the question the last bracket is not included in the code area.
Why a negative point for the effort of testing and finding an error?
0

I can't really test this because I have no URL to test it with, but you can try to set a header.

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($postData)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.