Kaizen #7 - URL Encoding while making API Calls

Kaizen #7 - URL Encoding while making API Calls

Welcome to another week of Kaizen. In this post, we will discuss things you need to handle in your code before you start making API calls to Zoho CRM. 

If you have an application that is integrated with Zoho CRM, it will contain the code for making API calls with different request methods—GET, POST, PUT, and DELETE. We will explain the things you need to handle for each of these methods, before you use it to make API calls to Zoho CRM.  

a. GET method

A GET method is used to retrieve data/resource from the resource server. For example, Search Records.

For the GET method, you need to pass data as a parameter in the request URL, and that parameter value has to be URL encoded. 

URL encoding is a way to encode or escape reserved, unprintable, or non-ASCII characters in URLs, and convert them into a secure format. 

Consider the following example, when you try to search a lead record with an email ID(p.daly+demo1@zylker.com).





When this request URL is decoded in our end, it gets converted into:

The '+' character got decoded as 'space'. Because, the '+' character is valid in URL as a substitute for space. Therefore, they have to be encoded or escaped. If not, the URL might be misinterpreted while decoding it. 

Unsafe characters that you need to encode are the following.
  • ASCII Control characters:
    ISO-8859-1 (ISO-Latin) character ranges 00-1F hex (0-31 decimal) and 7F (127 decimal.)
  • Non-ASCII characters:
    Entire “top half” of the ISO-Latin set 80-FF hex (128-255 decimal.)
  • Unsafe characters
    Blank/empty space and " < > # % { } | \ ^ ~ [ ] `
  • Reserved characters when NOT USED for their reserved or defined purposes such as / ? : = &.
Based on your language, you can use the following methods to encode the reserved characters. 

Language
Script
Java
URLEncoder.encode("(p.daly+demo1@zylker.com)", "UTF-8");
Ruby
URI::encode("(p.daly+demo1@zylker.com)")
PHP
Python
urllib.parse.quote("(p.daly+demo1@zylker.com)".encode("UTF-8"))

After encoding, the resultant search query will have the following email ID—p.daly%2Bdemo1%40zylker.com



The corresponding cURL code for this operation is:
curl -X GET \
-H 'Authorization: Bearer 1000.b481413bedf852e81bf591e7ca769de8.85f9671a36047d740216b656c8b3ad4d' 

You can see that the data in the URL is encoded. 

b. POST/PUT method

POST method is used to insert or upload any new resources to the server. For example, Insert Records.

PUT method is used to update an existing resource. This replaces the target resource with the updated content. For example, Update Records.

You can pass the input data for an API call in different ways, based on API requirements. 
  • Sending data as parameter in the request URL
  • Sending data wrapped in a request body
Let us look into each of these methods.  

a. Sending data as a parameter in the request URL 

As mentioned in the earlier section, when you send data as a parameter in  the request URL, it has to be encoded. 

b. Sending data wrapped in a request body

When data is sent in request body, it is secure. There are two different methods  in which you can send data wrapped in a request body, for an API call. Based on  the API requirement, you can choose the appropriate method. 

  • Sending data as raw JSON in the request body 
    In this type, the data is sent as JSON in the request body. Here, you need to add the header 'Content-Type' as 'application/json'. When data is sent as raw JSON, it need not be encoded
  • Sending data as a key-value pair 
    Here, the data is given as key-value pairs. For instance, while you upload an attachment, you need to send the file data, for which you need to make the request with 'Content-Type' as 'multipart/form-data'. Here the data is wrapped in request body. Therefore, it need not be encoded. If the data you have is already encoded, you must use the 'application/x-www-form-urlencoded' content-type and you cannot send file data with this content-type.



Consider the following example of inserting leads.



The corresponding cURL code is:
-H 'Authorization: Bearer 1000.b481413bedf852e81bf591e7ca769de8.85f9671a36047d740216b656c8b3ad4d' \
-H 'Content-Type: application/json' \
 -d '{
    "data": [
        {
            "Company": "Zylker",
            "Last_Name": "Daly",
            "First_Name": "Paul",
            "Email": "p.daly+demo1@zylker.com",
            "State": "Texas"
        },
        {
            "Company": "Villa Margarita",
            "Last_Name": "Bran",
            "First_Name": "Hofstadter",
            "Email": "bran.h+demo2@villa.com",
            "State": "Texas"
        }
    ],
    "trigger": [
        "approval",
        "workflow",
        "blueprint"
    ]
}'

You can see that the data is not encoded.

c. DELETE method

DELETE method is used to delete a resource at a particular location. For example, Delete Records.

URL encoding for the DELETE method is the same as the GET method. Consider the following example of deleting leads. 

Encoded request URL:



We hope you found this post useful. Keep a tab on this series for more exciting topics!

Reach out to us at support@zohocrm.com if you have any questions, or let us know in the comment section.

Cheers!



















    Access your files securely from anywhere

        Zoho Developer Community




                                  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 Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • 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