1

Actually i want to send get request to an api. the response is in xml. I have tried this

<article>
    <div class="entry-content">
        <?php

        $url = "XXXXXXX";

        $args = array(
            'headers' => array(
                'Authorization' => 'Basic ' . base64_encode( 'username:password' ),
                'Accept' => 'application/xml'
            )
        );

        $res = wp_remote_request(
            $url,
            array(
                'method'    => 'GET',
                'headers'   => $args
            )
        );

        print_r($res);
        ?>  
    </div>
</article>

I have tested the same url in postman and the response from server is coming. but in worpress, its not coming, its giving me 403 Requests_Utility_CaseInsensitiveDictionary data-protected error.

How to solve this please help me.

3
  • Hi! And welcome to the community! First of all, have you verified the encoded credentials are exactly equals? If you are debugging, you could show the encoded credentials first and the compare with the headers that postman is actually sending to the remote endpoint. Dec 7, 2021 at 17:57
  • can you please help me how to compare the encoded credentials Dec 8, 2021 at 4:39
  • yes i have checked the encoded creds both are same but its notworking Dec 8, 2021 at 5:00

1 Answer 1

1

Watch your keys:

       $headers = array(
           // 'headers' => array( // this is duplicated
                'Authorization' => 'Basic ' . base64_encode( 'username:password' ),
                'Accept' => 'application/xml'
           // )
        );

        $res = wp_remote_request(
            $url,
            array(
                'method'    => 'GET',
                'headers'   => $headers
            )
        );

Don't send headers inside headers. Using a variable name different from args can help you spot the issue.

Example reference in docs:

https://developer.wordpress.org/plugins/http-api/#get-using-basic-authentication

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.