Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Help with using API and http_fetch

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with using API and http_fetch

    I am trying to interact with an API called ItemMaster (https://api.itemmaster.com/v2/api). I have read a number of posts, but am obviously missing some key points of how to do this.
    Here is code, modeled after this post by Steve Wood( http://www.alphasoftware.com/alphafo...t=encoding+url), that I have tried both on an A5W page and in a UX component on a button using an Ajax callback:
    Code:
    <%a5 
    debug(1)
    dim t_gatewayurl as c	= "https://api.itemmaster.com/v2/brand" 
    
    
    req = "<?xml version='1.0' encoding='utf-8' ?>" + crlf() + \
    "<requests xmlns='http://www.w3.org/2001/XMLSchema'>" + crlf() + \
    "<username:xxxxx>" + crlf() + \
    "<password:xxxxxx>" + crlf() + \
    "</requests>"
    
    dim url as c
    dim body as c
    url = t_gatewayurl
    dim pm as p
    dim pm.host as c = a5_split_url(url).protocol + "//" + a5_split_url(url).server
    dim pm.page as c = a5_split_url(url).page
    dim pm.port as n = 443
    dim pm.method as c = "post"
    dim pm.header as c ="username:xxxxx""password:xxxxxxxx"
    dim pm.header as c =
    dim pm.ssl_on as l = .t.
    dim pm.body as c = req
    resp = http_fetch(pm) 
    %>
    I get this error, or a similar one: "Error:Could not connect to server: WSAHOST_NOT_FOUND - Host not found while getting host name.. Detailed Description: No such host is known. The name is not an official host name or alias, or it cannot be found in the database(s) being queried. This error may also be returned for protocol and service queries, and means that the specified name could not be found in the relevant database.

    Error Number:16052.000000"
    I am also confused by where to put the user name and password. The API documentation says to set both the username and password as separate http header parameters. Is this done in the request body, or using the pm.header parameter? If the latter, what is the correct way to specify multiple header parameters?
    Richard Urban

    Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

  • #2
    Re: Help with using API and http_fetch

    I am not sure of what else is wrong but this will not work.

    Code:
    dim pm.header as c ="username:xxxxx""password:xxxxxxxx"
    
    Maybe 
    dim pm.header as c ="username:xxxxx,password:xxxxxxxx"
    or
    dim pm.header as c ="username:xxxxx password:xxxxxxxx"
    Win 10 64 Development, Win 7 64 WAS 11-1, 2, Win 10 64 AA-1,2, MySql, dbForge Studio The Best MySQL GUI Tool IMHO. http://www.devart.com/dbforge/mysql/studio/

    Comment


    • #3
      Re: Help with using API and http_fetch

      Normally... working with an API is, at once, both an exciting and frustrating experience. The "Standards" that all RESTful APIs purport to use could fill a barn. The only standard thing about API implementation is that they're all different. Part of the frustration is that most examples are in Ruby or PHP or C or Java. Well... we don't live in that world. Even a Javascript example would be helpful, but rarely provided. However, many times we get a cURL example, as we do in this API.

      Alpha, once again, to the rescue.

      Once you're in the XBasic editor, right click and select Genies... and then choose CURL command to XBasic. See? It's exciting already.

      Paste your cURL command into CURL command line - it must be all one long line. For example... (itemMaster includes a $, but get rid of that)

      Code:
      curl --header "username:xxx" --header "password:yyy" 'https://api.itemmaster.com/v2/manufacturer'
      and click the Generate XBasic button. You'll get the results below. I've commented out the SSH_KNOWNHOSTS line... you don't need that.

      The returning data is in either the cf_1.GetHeaders() call or the cf_1.GetContent() call... probably content. Assign each to a pointer variable and see what's inside.

      Thank goodness Alpha has put cURL to XBasic in place.


      Code:
      dim cf_1 as extension::CurlFile
      dim flag_1 as l
      dim slist1[0] as c = [ "username:xxx" , "password:yyy" ]
      dim ce as extension::Curl
      
      ce = extension::Curl.Init()
      ce.setOpt("URL","https://api.itemmaster.com/v2/manufacturer")
      ce.setOpt("NOPROGRESS",1)
      ce.setOpt("USERAGENT","curl/7.34.0")
      ce.setOpt("HTTPHEADER",slist1)
      ce.setOpt("MAXREDIRS",50)
      ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
      ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
      ''''ce.setOpt("SSH_KNOWNHOSTS","C:\Users\David\AppData\Roaming/_ssh/known_hosts")
      ce.setOpt("TCP_KEEPALIVE",1)
      ce.SetOpt("FILE",cf_1)		
      flag_1 = ce.Exec()
      if flag_1 then
           showvar( "Headers: "+crlf()+cf_1.GetHeaders()+crlf()+"Content:"+crlf()+cf_1.GetContent())
      else 
      	 showvar("error: " + ce.Error() )
      end if
      ce.close()

      Comment


      • #4
        Re: Help with using API and http_fetch

        Your problem is here:
        Code:
        dim pm.host as c = a5_split_url(url).protocol + "//" + a5_split_url(url).server
        The host should be a hostname only, not a protocol and a hostname. The error you are getting is telling you that you have supplied an invalid hostname. Change your code to
        Code:
        dim pm.host as c = a5_split_url(url).server
        There is also a problem with pm.hostname as Frank pointed out, which will likely cause the next problem for you once you fix the host.
        Last edited by Lenny Forziati; 03-19-2015, 04:58 PM.

        Lenny Forziati
        Vice President, Internet Products and Technical Services
        Alpha Software Corporation

        Comment


        • #5
          Re: Help with using API and http_fetch

          I could not get the code in the first post to work after correcting the host: the message returned is:
          Response: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
          <html><head>
          <title>302 Found</title>
          </head><body>
          <h1>Found</h1>
          <p>The document has moved <a href="https://api.itemmaster.com/v2">here</a>.</p>
          </body></html>

          I was able to get the Curl command to run properly from the windows command screen, when using the --insecure command line option like this:
          Code:
          curl --insecure --header "username:xxx" --header "password:yyy" 'https://api.itemmaster.com/v2/brand'
          Without the --insecure, the command line said there was a problem validating the certificate file.

          Using the Curl genie with the above code, I get the following code (after manually adding in the CA INFO and CA Path, which were wrong)

          Code:
          dim cf_1 as extension::CurlFile
          dim cf_2 as extension::CurlFile
          dim cf_3 as extension::CurlFile
          dim cf_4 as extension::CurlFile
          dim cf_5 as extension::CurlFile
          dim flag_1 as l
          dim flag_2 as l
          dim flag_3 as l
          dim flag_4 as l
          dim flag_5 as l
          dim slist1[0] as c = [ "username:xxx" , "password:xxx" ]
          dim slist2[0] as c = [ "username:xxx" , "password:xxx" ]
          dim slist3[0] as c = [ "username:xxx" , "password:xxx" ]
          dim slist4[0] as c = [ "username:xxx" , "password:xxx" ]
          dim slist5[0] as c = [ "username:xxx" , "password:xxx" ]
          dim ce as extension::Curl
          
          ce = extension::Curl.Init()
          ce.setOpt("URL","https://api.itemmaster.com/v2/brand")
          ce.setOpt("NOPROGRESS",1)
          ce.setOpt("USERAGENT","curl/7.33.0")
          ce.setOpt("HTTPHEADER",slist1)
          ce.setOpt("MAXREDIRS",50)
          ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
          ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
          ce.setOpt("SSL_VERIFYPEER",0)
          ce.setOpt("SSL_VERIFYHOST",0)
          ce.setOpt("TCP_KEEPALIVE",1)
          ce.SetOpt("FILE",cf_1)		
          flag_1 = ce.Exec()
          ce.setOpt("URL","Files")
          ce.setOpt("NOPROGRESS",1)
          ce.setOpt("USERAGENT","curl/7.33.0")
          ce.setOpt("HTTPHEADER",slist2)
          ce.setOpt("MAXREDIRS",50)
          ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
          ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
          ce.setOpt("SSL_VERIFYPEER",0)
          ce.setOpt("SSL_VERIFYHOST",0)
          ce.setOpt("TCP_KEEPALIVE",1)
          ce.SetOpt("FILE",cf_2)		
          flag_2 = ce.Exec()
          ce.setOpt("URL","(x86)\a5V12\caroot\ca-cert.pem")
          ce.setOpt("NOPROGRESS",1)
          ce.setOpt("USERAGENT","curl/7.33.0")
          ce.setOpt("HTTPHEADER",slist3)
          ce.setOpt("MAXREDIRS",50)
          ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
          ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
          ce.setOpt("SSL_VERIFYPEER",0)
          ce.setOpt("SSL_VERIFYHOST",0)
          ce.setOpt("TCP_KEEPALIVE",1)
          ce.SetOpt("FILE",cf_3)		
          flag_3 = ce.Exec()
          ce.setOpt("URL","Files")
          ce.setOpt("NOPROGRESS",1)
          ce.setOpt("USERAGENT","curl/7.33.0")
          ce.setOpt("HTTPHEADER",slist4)
          ce.setOpt("MAXREDIRS",50)
          ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
          ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
          ce.setOpt("SSL_VERIFYPEER",0)
          ce.setOpt("SSL_VERIFYHOST",0)
          ce.setOpt("TCP_KEEPALIVE",1)
          ce.SetOpt("FILE",cf_4)		
          flag_4 = ce.Exec()
          ce.setOpt("URL","(x86)\a5V12\caroot")
          ce.setOpt("NOPROGRESS",1)
          ce.setOpt("USERAGENT","curl/7.33.0")
          ce.setOpt("HTTPHEADER",slist5)
          ce.setOpt("MAXREDIRS",50)
          ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
          ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
          ce.setOpt("SSL_VERIFYPEER",0)
          ce.setOpt("SSL_VERIFYHOST",0)
          ce.setOpt("TCP_KEEPALIVE",1)
          ce.SetOpt("FILE",cf_5)		
          flag_5 = ce.Exec()
          if flag_1 then
               showvar( "Headers: "+crlf()+cf_1.GetHeaders()+crlf()+"Content:"+crlf()+cf_1.GetContent())
          else 
          	 showvar("error: " + ce.Error() )
          end if
          if flag_2 then
               showvar( "Headers: "+crlf()+cf_2.GetHeaders()+crlf()+"Content:"+crlf()+cf_2.GetContent())
          else 
          	 showvar("error: " + ce.Error() )
          end if
          if flag_3 then
               showvar( "Headers: "+crlf()+cf_3.GetHeaders()+crlf()+"Content:"+crlf()+cf_3.GetContent())
          else 
          	 showvar("error: " + ce.Error() )
          end if
          if flag_4 then
               showvar( "Headers: "+crlf()+cf_4.GetHeaders()+crlf()+"Content:"+crlf()+cf_4.GetContent())
          else 
          	 showvar("error: " + ce.Error() )
          end if
          if flag_5 then
               showvar( "Headers: "+crlf()+cf_5.GetHeaders()+crlf()+"Content:"+crlf()+cf_5.GetContent())
          else 
          	 showvar("error: " + ce.Error() )
          end if
          ce.close()
          However, unlike from the command line, this does not run, instead I get the get the attached error saying "Function 'not valid' was not recognized".

          One last point, I noticed that in the windows command line you have to use double quotes around the URL, instead of single quotes. However, the x-basic genie comes up with double quotes around single quotes for the URL; is that right? I also tried with just the double quotes.
          "
          "
          Last edited by richardurban; 01-17-2015, 05:26 AM.
          Richard Urban

          Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

          Comment


          • #6
            Re: Help with using API and http_fetch

            Here is the error mentioned above: curl error1.JPG
            Richard Urban

            Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

            Comment


            • #7
              Re: Help with using API and http_fetch

              I'm not sure where you're getting the "--insecure" stuff, but if you take itemMaster's cURL example...

              Code:
              curl --header "username:xxx" --header "password:yyy" 'https://api.itemmaster.com/v2/brand'
              The genie will produce this code...

              Code:
              dim cf_1 as extension::CurlFile
              dim flag_1 as l
              dim slist1[0] as c = [ "username:xxx" , "password:xxx" ]
              dim ce as extension::Curl
              
              ce = extension::Curl.Init()
              ce.setOpt("URL","https://api.itemmaster.com/v2/brand")
              ce.setOpt("NOPROGRESS",1)
              ce.setOpt("USERAGENT","curl/7.33.0")
              ce.setOpt("HTTPHEADER",slist1)
              ce.setOpt("MAXREDIRS",50)
              ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
              ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
              ce.setOpt("SSL_VERIFYPEER",0)
              ce.setOpt("SSL_VERIFYHOST",0)
              ce.setOpt("TCP_KEEPALIVE",1)
              ce.SetOpt("FILE",cf_1)		
              flag_1 = ce.Exec()
              
              if flag_1 then
              	dim cfh1 as c
              	dim cfc1 as c
              	cfh1 = cf_1.GetHeaders()
              	cfc1 = cf_1.GetContent()	
              end if	
              ce.close()
              If you're getting more than one set of vars and code then something is wrong with your cURL command.

              The error you're getting is because showvar is only valid in the Interactive window. If you want to run your code there, then fine. If you're running your code in a function, then replace the showvar stuff with code as in the sample above. My code is in the "if flag_1" test. Just assign the header and content data to variables to easily check what's inside.

              Comment


              • #8
                Re: Help with using API and http_fetch

                With Alpha adding CURL is there now something fundamentally wrong with using the much more simple syntax in the first post, http_fetch(pm)? CURL has been available to Alpha apps since the beginning of time but up to this point everyone used http_fetch() or http_post() to address web services.
                Steve Wood
                Join the ALPHA DEVELOPERS NETWORK
                There is no Cloud. It's just someone else's computer.
                Web - Mobile - Hosting - Products - Frameworks - Developer Resources
                AlphaToGo | IADN (100% Alpha Anywhere Websites)

                Comment


                • #9
                  Re: Help with using API and http_fetch

                  It really depends (IMO) on the site, as to how easy or hard getting the fetch or post to work. For someone who is new to RESTful services, the cURL to XBasic Genie gets you there a lot faster. Most RESTful sites give you a cURL example and not a simple Fetch or Post example. It's tough, at times, to work out what goes in the header, what goes in the body, etc.

                  Comment


                  • #10
                    Re: Help with using API and http_fetch

                    Rich,

                    showvar() is a desktop only function and will error out on the web side. Running the code you show, with the showvar() method, from the desktop, produces an "error: Couldn't resolve host name".
                    Jim Coltz
                    Alpha Custom Database Solutions, LLC
                    A5CustomSolutions.com
                    jimcoltz@a5customsolutions.com

                    Comment


                    • #11
                      Re: Help with using API and http_fetch

                      This works in the interactive window with the new shell command functionality:
                      Code:
                      $curl --insecure --header  "username:xxx" --header "password:xxxxx" "https://api.itemmaster.com/v2/item?upc=012000004926&epf=50" --output "C:\Temp\testfile.txt"
                      I then get a retun file with the url of the image to download.
                      However, I don't know how to download the image: when I try this
                      Code:
                       --insecure  --header  "xxxxx" -- --header "xxxxxx" "http://media.itemmaster.com/0/0/0/401/c4b13721-eb7d-4251-995a-bc5f3c8bd925.jpg" -o "c:\temp\picturetest.jpg"
                      A message is returned saying:
                      HTTP Status 407 The client must first authenticate itself with the proxy.
                      Does anyone know the correct format to use to first authenticate with CURL and then download the image at a given url?
                      Richard Urban

                      Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

                      Comment


                      • #12
                        Re: Help with using API and http_fetch

                        Hey Richard,

                        I think this is what you're looking for. This falls under the heading of... sometimes... this stuff is just too much fun. Imagine there being a web service that does this. Too funny.

                        http://www.youtube.com/watch?v=9EZhxP3MmEc

                        I'll package and post the code.

                        Comment


                        • #13
                          Re: Help with using API and http_fetch

                          Very nice.... A tremendous amount of potential.

                          If only they captured tare weight, too...

                          Comment


                          • #14
                            Re: Help with using API and http_fetch

                            It seems they have it, but it's not included in the xml data that I can see. E.g. a 1lb box of Ritz crackers has the packaging listed at 1.195lb. They only list 1lb in the xml data. They'd probably add it to the xml if asked.

                            Comment


                            • #15
                              Re: Help with using API and http_fetch

                              I enjoyed their video (on YouTube) where they created a virtual PeaPod store.

                              I can think of 100 uses for such an API if it was nearly complete. It looks like they have a long way to go. Some products, like Heinz, are near 100%. Some, Like Absolut, are just a single of dozens of retail items.

                              I wonder why they stopped at liquor and food? Why not any retail item?

                              Anyway, defineatly going in the right direction.

                              Comment

                              Working...
                              X