Building a custom API from scratch with custom objects

Have more questions? Submit a request

10 Comments

  • Azhar
    Comment actions Permalink

    While posting the data using API request to create a doggo record, we are getting 404 error. We have checked all the configurations and files and it looks fine to us as per the article above.

    Getting below error :

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html>
    <head>
    <title>Error: 404 Not Found</title>
    <style type="text/css">
    html {background-color: #eee; font-family: sans-serif;}
    body {background-color: #fff; border: 1px solid #ddd;
    padding: 15px; margin: 15px;}
    pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
    </style>
    </head>
    <body>
    <h1>Error: 404 Not Found</h1>
    <p>Sorry, the requested URL
    <tt>&#039;http://localhost:8080/api/doggos&#039;</tt>
    caused an error:
    </p>
    <pre>Not found: &#039;/api/doggos&#039;</pre>
    </body>
    </html>

    0
  • Charles Nadeau
    Comment actions Permalink

    Hi Azhar,

    404 means the server couldn't find the file, which in Bottle terms means it couldn't find a route to the endpoint. Can you check the endpoint's route in your doggo_api.py file. It should be:

    @route('/api/doggos', method='POST')

    0
  • Azhar
    Comment actions Permalink

    Hi Charles,

    Yes, the endpoint route in my doggo_api.py is exactly same as what you mentioned but I am receiving the 404 error every time I am running the python file normally and hitting the POST or GET request either form POSTMAN or CURL. 

    Whenever I am executing the doggo_api.py file in debug mode using -m pdb then for the first time when the code reaches on fetching json request after establishing connection getting failed  but upon several attempt  it is able to post successfully and I am receiving the response  i.e. only when debug mode is on and connection getting establish upon repeating after failing once, we are receiving the response. I am exactly clueless of this weird situation now.

     

    Normal Scenrio :

     

    Debug Mode Scenario  (after repetitive attempt):

     

    We want flawless consistent response. Need help in this regards.

    0
  • Charles Nadeau
    Comment actions Permalink

    Strange. I retested the app this morning and it still works for me. I notice in your Postman screenshots that you included the "-v" verbose flag in the endpoint URL. The field is a web address, not a curl statement. The extra "-v" will return a 404 error. You didn't include the -v flag in the example that worked.

    0
  • Azhar
    Comment actions Permalink

    That -v  might be due to the copy-paste. But it didn't worked for me without that flag too in normal scenario. Please find the screen shot below :

     

    Let me know if I can attach my files somewhere which I am running to achieve the result,so that you can simply run my file and can detect if by mistake I have been doing something incorrect in the file.

     

    0
  • Suman Saha
    Comment actions Permalink

    Hi Charles,

    I have created this api and is working fine. I'm able to access data with api calls for e.g.

    http://localhost:8080/api/owners/407611529753/doggos

    Now I need to call the same API from a custom app, that I have built in Zendesk Support and display the data there for the same user (with id 407611529753).

    How do I call the API?

    I tried the below but it did not work. 

    function requestUserInfo(client, id) {
    var settings = {
    //url: '/api/v2/users/' + id + '.json',
    url: 'api/sunshine/objects/records/zen:user:' + id + '/related/owner_has_many_doggos',
    type:'GET',
    dataType: 'json',
    };

    Can you pls help me or direct me to some documentation where I can find some help.

    Also just a thought is it something to do with the datatype that is getting returned?

    the same urlis working in Postman:

    Thanks,

    Suman

    0
  • Charles Nadeau
    Comment actions Permalink

    Hi Suman,

    I noticed a slash is missing at the beginning of the url property:

    url: 'api/sunshine/objects/records/zen:user:' + id + '/related/owner_has_many_doggos',

    Maybe that's the culprit? Can you try:

    url: '/api/sunshine/objects/records/zen:user:' + id + '/related/owner_has_many_doggos',
    0
  • Banhi Kumar Ratha
    Comment actions Permalink

    Hi Charles,

    I called sunshine api from Support sidebar its working fine but when I tried to call from Zendesk Sell sidebar , I am getting error.

    My javascript code:

    var issueInfoRequest = {
                 url: "/api/sunshine/objects/records?type=[type]",
                 dataType: 'json',
                 type: 'GET'   };

    client.request(issueInfoRequest).then(function(response) {
                console.log(response)
    });

    Can you please help on this.

    Note: From support sidebar its working

     

    Thanks 

    Banhi

    0
  • Bryan - Community Manager
    Comment actions Permalink

    Hello Banhi Kumar Ratha. It's not clearly documented but but the ZAFClient request method is not fully supported in Sell apps to call either Sunshine or Zendesk Apps proxy (ZAP) endpoints.

    You can, however, directly access Sunshine APIs by including an Authorization header with a Bearer token using something like:

        var settings = {
    url: 'https://your_subdomain.zendesk.com/api/sunshine/objects/records?type=device',
    type: 'GET',
    dataType: 'json',
    cors:true,
    headers: {
    "Authorization":"Bearer b103260d80b103260d80b103260d80b103260d80b103260d80b103260d80"
    }
    }

    client.request(settings).then(
    function(data) {
    console.log("success:", data);
    },
    function(response) {
    console.log("error:", data);
    }
    )

    Note, because it's a cross-origin call, the call has to use an OAuth Bearer access token — not a "Basic" username/password value.

    For other caveats and limitations, see Known app limitations. This article may also be interesting to have as a reference for other work — it's more Zendesk Support specific, but may still be helpful: How Apps framework client.request works.

    The above workaround will not be ideal for everyone. This limitation is known to the product team but I can't say when (or technically even if) this will be addressed. I'm hoping this at least sets expectations and gets you to your next steps!

    0
  • Jon Simone
    Comment actions Permalink

    Followed article and it seems like it may need to be updated. I am receiving the following error when running the python3 create_doggo_type.py command for the first time:

    python3 create_doggo_type.py

    Traceback (most recent call last):

      File "/Users/jonsimone/Coding/doggogo/create_doggo_type.py", line 40, in <module>

        response = sunshine.create_object_type(key, schema)

    NameError: name 'sunshine' is not defined

    I checked on the github files and they do not name the first file zendesk.py but sunshine.py instead. I am new to this so I am wondering if this makes a difference. Any help is appreciated. 

    Also wondering how to authorize using a token instead of user name and password in sunshine.py / zendesk.py.

    Edit: Renaming zendesk.py to sunshine.py seemed to help but create_doggo_type.py needs to import sunshine and not zendesk. 

    Also for credentials just convert username\token: to base64 and add it like this: 

    credentials = '_base64token_'

    ________________

    Having done all of this I am now encountering a new problem when running create_doggo_type.py 

    Traceback (most recent call last):

      File "/Users/jonsimone/Coding/doggogo/create_doggo_type.py", line 42, in <module>

        response = sunshine.create_object_type(key, schema)

      File "/Users/jonsimone/Coding/doggogo/sunshine.py", line 12, in create_object_type

        response = requests.post(url, json=data, auth=credentials, headers=headers)

      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/api.py", line 119, in post

        return request('post', url, data=data, json=json, **kwargs)

      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/api.py", line 61, in request

        return session.request(method=method, url=url, **kwargs)

      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/sessions.py", line 528, in request

        prep = self.prepare_request(req)

      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/sessions.py", line 456, in prepare_request

        p.prepare(

      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/models.py", line 320, in prepare

        self.prepare_auth(auth, url)

      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/models.py", line 551, in prepare_auth

        r = auth(self)

    TypeError: 'str' object is not callable

    Any ideas?

    0

Please sign in to leave a comment.

Powered by Zendesk