PUT /api/v2/requests/{request_id} CORS issue with 'X-On-Behalf-Of' header | Community
Skip to main content

PUT /api/v2/requests/{request_id} CORS issue with 'X-On-Behalf-Of' header

  • June 23, 2023
  • 3 replies
  • 0 views

Zachary13
const submitComment = async () => {

    try {

    const data: any = { request: { comment: { body: "This is my new comment on my ticket" } };

      const authHeaders = {

        'Content-Type': 'application/json',

        'Authorization': process.env.ZENDESK_OAUTH_API_KEY,

      'X-On-Behalf-Of': user.email

      };

//https://{DOMAIN}.zendesk.com/api/v2/requests/:requestId

    const response = await axios.put(`${ZENDESK_API}/requests/${params.id}`, data, { headers: authHeaders });

      console.log(response);

    } catch (error) {

      console.log(error);

    }

  }
 
I used this documentation to generate an access token to be used to call the Zendesk API:
 
Using the 'X-On-Behalf-Of' works just fine with POSTMAN but in my web app I am getting CORS error:
 
Access to XMLHttpRequest at 'https://{DOMAIN}.zendesk.com/api/v2/requests/460105' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field x-on-behalf-of is not allowed by Access-Control-Allow-Headers in preflight response.
 
I was wondering if there is some configuration in Zendesk I am missing to allow for CORS.  From my understanding the access token generated should support CORS but I do believe the documentation also states that any errors related to the API call could throw CORS errors.  Removing the 'X-On-Behalf-Of' of course gives me a 200 response but I was hoping to get this to work with using the 'X-On-Behalf-Of'. 
 
Maybe there are other solutions to this problem without supplying the end user name and password into basic auth to update the request with a comment.  Most of our users do not verify their email and mostly only update requests through email.  
 
Thanks,
Zach

3 replies

Hi Zachary,
 
I'd like to see whether this is related to the local test of your web app.  Does your dev environment have a hosted version of the app to test sending the request from?

Zachary13
  • Author
  • June 26, 2023

Hi Christopher, 

Unfortunately, putting it on a live site did not remedy the problem:

Access to XMLHttpRequest at 'https://{DOMAIN}.zendesk.com/api/v2/requests/460108' from origin 'https://{SAME DOMAIN NAME}.com' has been blocked by CORS policy: Request header field x-on-behalf-of is not allowed by Access-Control-Allow-Headers in preflight response.

Thanks for the confirmation.  I've created a ticket so we can take a closer look at the details of your tests together.  I'll follow up with you through that ticket.