The real problem causing many of the Error 400: Invalid Data error messages with CORS... and a temporary workaround.

The real problem causing many of the Error 400: Invalid Data error messages with CORS... and a temporary workaround.

After having just spent the last three days debugging four lines of code, I'm happy to say that I believe I've discovered the root cause that may be responsible for many of the CORS problems regularly being presented in the forums - especially for those who use custom functions with API keys.

Furthermore, this may technically be more of a poor implementation of CORS by the browsers rather than with Zoho not following spec.  By that I mean that I believe that Zoho's implementation is technically correct, but a simple and inconsequential update can easily address this issue.

The problem is simply this... If you are attempting to fetch data from a CRM custom function from your Javascript code, it's likely that your browser is automatically adding in the header for Origin.

If you're going by the book, this should be the server name, like www.yourdomain.com.  In this regard, I believe that Zoho has a technically correct implementation.  But... it appears that the Origin header is set and/or overridden by the browser as https://www.yourdomain.com/.  Further frustrating the problem is that short of having a CORS browser extension, it would seem to be the case that there is no way of removing the https:// from the Origin.

So what happens when the Origin header is presented to Zoho's servers prefixed with https://?  It will fail every time with Error 400: Invalid Data.  This makes it nearly impossible to implement custom functions called via APIKey authentication into your Javascript code, and or likely even possibly functions using OAuth2.

So the solution is simple... Someone just needs to call up to Google and kindly explain to them that they've made a mistake and that the Origin header being added falls outside specification, and you'll be looking forward to seeing this fixed in the next public release of Chrome.













Bwahahahah.  I'm just kidding.    Even if a fix to correct the issue also magically could save the lives of 100 baby kittens...



...it would still probably never happen in Chrome, as having a 70% market-share affords them the leverage to do things however they like.  Sorry cats.

So the more realistic fix is for Zoho to simply remove the http:// and https:// prefixes if they're presented in the Origin headers.

And I would hope this could happen soon, as I don't want to bring out the puppy photos to further illustrate my point.

Lastly, for those who need an immediate solution, I recommend spinning up a Heroku server with the CORS proxy server known as cors-anywhere.  When I have the time, I'm going to see if i can get it ported over to CRM implementation of node.js, but in the meantime, there a few step-by-step guides online of how to get it up and running in your own private Heroku serverless instance , which you can run for free.

But... Cors-anywhere alone won't solve the problem, as it still retains whatever header is provided to it by the browser.  It does however provide an intermediary step between the browser and Zoho's servers to remove the http:// and https:// prefixes so that only the server location remains.

Once you get an instance of cors-anywhere up functioning as it should, just replace the cors-anywhere.js file in the lib folder with the version I've attached here and your origin headers will automagically be transformed into Zoho-friendly origins.  Oh... and by the way... it gets rid of those pesky little CORS errors at the same time.

Hope this helps.

-Bryan




                            Zoho Desk Resources

                            • Desk Community Learning Series


                            • Digest


                            • Functions


                            • Meetups


                            • Kbase


                            • Resources


                            • Glossary


                            • Desk Marketplace


                            • MVP Corner


                            • Word of the Day



                                Zoho Marketing Automation


                                        Manage your brands on social media



                                                Zoho TeamInbox Resources

                                                  Zoho DataPrep Resources



                                                    Zoho CRM Plus Resources

                                                      Zoho Books Resources


                                                        Zoho Subscriptions Resources

                                                          Zoho Projects Resources


                                                            Zoho Sprints Resources


                                                              Qntrl Resources


                                                                Zoho Creator Resources


                                                                  Zoho WorkDrive Resources



                                                                    Zoho Campaigns Resources

                                                                      Zoho CRM Resources

                                                                      • CRM Community Learning Series

                                                                        CRM Community Learning Series


                                                                      • Tips

                                                                        Tips

                                                                      • Functions

                                                                        Functions

                                                                      • Meetups

                                                                        Meetups

                                                                      • Kbase

                                                                        Kbase

                                                                      • Resources

                                                                        Resources

                                                                      • Digest

                                                                        Digest

                                                                      • CRM Marketplace

                                                                        CRM Marketplace

                                                                      • MVP Corner

                                                                        MVP Corner

                                                                      





                                                                      




                                                                          Design. Discuss. Deliver.

                                                                          Create visually engaging stories with Zoho Show.

                                                                          Get Started Now