Changing access/jwt from get to post | Community
Skip to main content

Changing access/jwt from get to post

  • March 4, 2024
  • 2 replies
  • 0 views

sandhya

When using get method it is working properly whereas when converting it to post it returns different html in response than the expected page html. return_to is always empty in our case. Is there anything missing

$curl = curl_init();

$postData = json_encode(['jwt' => $token,'return_to'=>$return_to]);$x= mb_strlen($postData);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://yoursubdomain.zendesk.com/access/jwt',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>$postData,

));

$response = curl_exec($curl);

2 replies

  • March 4, 2024

@sandhya

From the code snippet you've provided, it seems like you're trying to make a POST request to the JWT endpoint. However, there are a few things to check and keep in mind:

  1. Make sure that you're setting the Content-Type header to application/json since you're sending JSON data.

  2. If return_to is always empty, you need to provide a valid URL to which Zendesk should redirect the user after successful authentication. If it's empty or invalid, Zendesk might not know where to redirect the user.

  3. It's important to check if there are any errors after the curl_exec call. You can check if curl_exec returns false and use curl_error to get the error message.

An example that may point you in the right direction:

$curl = curl_init();

$postData = json_encode([
    'jwt' => $token,
    'return_to' => $return_to // Make sure this is a valid URL
]);

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://yoursubdomain.zendesk.com/access/jwt',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 60,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $postData,
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Content-Length: ' . mb_strlen($postData)
    ),
));

$response = curl_exec($curl);

if ($response === false) {
    // Handle error; curl_exec returned false
    $error = curl_error($curl);
    // Log or echo the error message
    echo "cURL Error: $error";
} else {
    // Process your response
}

curl_close($curl);

sandhya
  • Author
  • March 5, 2024

By providing valid return url , it is giving some different page html in response . Moreover if I add   Content-Type in header it gives me 404 error. 
In case of get request also we are not passing return url , but it's working fine .
Further what response we will be getting in response of POST request , an html content of which page?