0

I am using Guzzle as my PHP HTTP client to perform api-calls. This worked great so far, for retrieving my access-token for example.

However, now that I am trying to perform a query search I get a strange Error:

SELECT+Name+FROM+Account\n ^\nERROR at Row:1:Column:6\n unexpected token: '+'

This is my code for performing my request:

try {
            $response = $client->get( 'services/data/v55.0/query/', [
                RequestOptions::HEADERS => [
                    'Authorization' => 'Bearer ' . $accessToken,
                    'Content-Type' => 'application/json'
                ],
                'query' => [
                    'q' => 'SELECT+Name+FROM+Account',
                ],
                /*'debug' => true*/
            ] );

            $data = json_decode( $response->getBody(), true );

            return $data;

        }
        catch ( \Exception $exception ) {
            echo $exception;
            /*refreshAccessToken();*/
            /*continue;*/
        }

As pointed out by @sfdcfox, guzzle encodes my query string. Unfortunately, when I pass my query directly to my url, or append it to the request using the JSON requestOption it does not get recognized by Salesforce. How can I perform my query with guzzle without encoding my query, and is it even possible?

New contributor
Fewture Webdesign is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

1

Guzzle uses http_build_query, which in turn automatically encodes characters for you using PHP_QUERY_RFC1738 by default. In other words, your literal + are being encoded, and on the far end, when decoded, becomes the literal query SELECT+Name+FROM+Account, which isn't valid SOQL. Just write the query "normally", and it should work.

try {
    $response = $client->get( 'services/data/v55.0/query/', [
        RequestOptions::HEADERS => [
            'Authorization' => 'Bearer ' . $accessToken,
            'Content-Type' => 'application/json'
        ],
        'query' => [
            'q' => 'SELECT Name FROM Account',
        ],
        /*'debug' => true*/
    ] );

    $data = json_decode( $response->getBody(), true );

    return $data;

}
catch ( \Exception $exception ) {
    echo $exception;
    /*refreshAccessToken();*/
    /*continue;*/
}
5
  • Thank you for the fast reply!! What exactly do you mean by writing the query "normally"? Jun 23 at 17:33
  • @FewtureWebdesign Just plain, unencoded text. The framework takes care of encoding special chars for you.
    – sfdcfox
    Jun 23 at 17:36
  • Does that mean, that I can not use Guzzle for this? I have no clue where to put the query string. If I put it outside the 'query' request option, it does not get recognized by Salesforce. Jun 23 at 17:42
  • @FewtureWebdesign 'q' => 'SELECT+Name+FROM+Account', should be 'q' => 'SELECT Name FROM Account', My apologies if that was unclear.
    – sfdcfox
    Jun 23 at 18:18
  • You helped me a lot, thank you for that!! It worked perfectly! Jun 23 at 18:57

Your Answer

Fewture Webdesign is a new contributor. Be nice, and check out our Code of Conduct.

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.