Results 1 to 8 of 8

Thread: [RESOLVED] XMLHTTP GET, passing API parameters

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Resolved [RESOLVED] XMLHTTP GET, passing API parameters

    Hi

    When accessing a Web API using http(s) you can do it a couple of ways i gather, the simplest way is to just put a URL with all the parameters and send it.

    The other way im trying to achieve it is to put all the parameters into the header and Body!??

    so im looking at 'RocketReach.com' API, it says in the documentation that you can place the KEY into the 'Header' and the search criteria into the 'Body'

    i cant find a 'Body' (a request one) in the xmlhttp object, am I getting this terribly wrong and by 'Body' it just wants me to put it into the URL?
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  2. #2

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: XMLHTTP GET, passing API parameters

    Quote Originally Posted by wqweto View Post
    The body of the request is passed as the first argument to the Send method.
    Thanks, yes i found that. however i couldnt make it work, i tried alsort of variation in the string and arrays and alsort, i cant find documentation on how to do this either thats not java or python.

    This is the instructions from the site.

    Code:
    curl --request 'GET' --location 'https://api.rocketreach.co/v2/api/lookupProfile'\
         --header 'Api-Key: 3e7k0123456789abcdef0123456789abcdef'\
         --data-urlencode 'name=Marc Benioff'\
         --data-urlencode 'current_employer=Salesforce'
    I just have no idea how to add 2 items into this body, i tried arrays, dictionaries, strings. the API always fails, I did notice this is case sensitive (this API anyway) and i found a few of my own typos which i may have missed while i was testing various methods, im just wandering if anyone has done this before.

    I have set it up to work the simple way as of now

    Code:
    Public Function Profile_Get(APIKey As String, Name As String, Employer As String) As String
        'Dim xmlhttp As Object
        'Set xmlhttp = CreateObject("MSXML2.serverXMLHTTP")
        Dim XMLHTTP As New MSXML2.XMLHTTP60
        
        Dim NameEnc As String
        Dim EmployerEnc As String
        Dim Param As String
        Dim URL As String
            
        NameEnc = Application.WorksheetFunction.EncodeURL(Name)
        EmployerEnc = Application.WorksheetFunction.EncodeURL(Employer)
        Param = "name=" & NameEnc & "&" & "current_employer=" & EmployerEnc
        
        URL = BASEURL & ENDPOINT_LOOKUPPROFILE & "?" & Param
        
        XMLHTTP.Open "GET", URL, False
        XMLHTTP.setRequestHeader "Api-Key", APIKey
        XMLHTTP.send
        
        Profile_Get = XMLHTTP.responseText
        
        Set XMLHTTP = Nothing
        
    End Function
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  4. #4
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,422

    Re: XMLHTTP GET, passing API parameters

    I just added -v parameter for verbose mode and got this

    Code:
    d:\TEMP\curl-7.69.1_1-win32-mingw>curl --request "GET" --location "https://api.rocketreach.co/v2/api/lookupProfile" --header "Api-Key: 3e7k0123456789abcdef0123456789abcdef" --data-urlencode "name=Marc Benioff" --data-urlencode "current_employer=Salesforce" -v
    *   Trying 35.82.157.241:443...
    * Connected to api.rocketreach.co (35.82.157.241) port 443 (#0)
    * ALPN, offering h2
    * ALPN, offering http/1.1
    * successfully set certificate verify locations:
    *   CAfile: d:\TEMP\curl-7.69.1_1-win32-mingw\curl-ca-bundle.crt
      CApath: none
    * TLSv1.3 (OUT), TLS handshake, Client hello (1):
    * TLSv1.3 (IN), TLS handshake, Server hello (2):
    * TLSv1.2 (IN), TLS handshake, Certificate (11):
    * TLSv1.2 (IN), TLS handshake, Server key exchange (12):
    * TLSv1.2 (IN), TLS handshake, Server finished (14):
    * TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
    * TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
    * TLSv1.2 (OUT), TLS handshake, Finished (20):
    * TLSv1.2 (IN), TLS handshake, Finished (20):
    * SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
    * ALPN, server accepted to use h2
    * Server certificate:
    *  subject: CN=rocketreach.co
    *  start date: May 13 00:00:00 2021 GMT
    *  expire date: Jun 11 23:59:59 2022 GMT
    *  subjectAltName: host "api.rocketreach.co" matched cert's "*.rocketreach.co"
    *  issuer: C=US; O=Amazon; OU=Server CA 1B; CN=Amazon
    *  SSL certificate verify ok.
    * Using HTTP2, server supports multi-use
    * Connection state changed (HTTP/2 confirmed)
    * Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
    * Using Stream ID: 1 (easy handle 0x354aac8)
    > GET /v2/api/lookupProfile HTTP/2
    > Host: api.rocketreach.co
    > user-agent: curl/7.69.1
    > accept: */*
    > api-key: 3e7k0123456789abcdef0123456789abcdef
    > content-length: 47
    > content-type: application/x-www-form-urlencoded
    So you have to be aware/know that curl by default uses "application/x-www-form-urlencoded" for content type (and this is not something that every API documentation clearly states) so you have to explicitly call XMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" in your code.

    Then just don't pass your constructed Param in the URL but in request body i.e. first argument of Send method. I guess both might work if the API inbound request handler is lax on the parameter transport (URL vs Body) -- try it yourself.

    cheers,
    </wqw>

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: XMLHTTP GET, passing API parameters

    OK, i am going to try a few things here

    ill add the header for the content type
    ill try a single parameter first
    ill try 2 parameters

    i have a question regarding the URLEncoded part, If i use the URL directly it doesnt use a fully encoded url (as for as the worksheet function works) the worksheet function encodes all the signs and symbols where as the URL in the example only seems to encode the spaces. this seems a little confusing to me. any thoughts?
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: XMLHTTP GET, passing API parameters

    Great!!

    This works

    Code:
    Public Function Profile_Get(APIKey As String, Name As String, Employer As String) As String
        'Dim xmlhttp As Object
        'Set xmlhttp = CreateObject("MSXML2.serverXMLHTTP")
        Dim XMLHTTP As New MSXML2.XMLHTTP60
        
        Dim NameEnc As String
        Dim EmployerEnc As String
        Dim Param As String
        Dim URL As String
            
        NameEnc = Application.WorksheetFunction.EncodeURL(Name)
        EmployerEnc = Application.WorksheetFunction.EncodeURL(Employer)
        Param = "name=" & NameEnc & "&" & "current_employer=" & EmployerEnc
        
        URL = BASEURL & ENDPOINT_LOOKUPPROFILE & "?" & Param
        
        XMLHTTP.Open "GET", URL, False
        XMLHTTP.setRequestHeader "Api-Key", APIKey
        XMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        XMLHTTP.send Param
        
        Profile_Get = XMLHTTP.responseText
        
        Set XMLHTTP = Nothing
        
    End Function
    Thanks @wqweto
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: XMLHTTP GET, passing API parameters

    Just before i mark the thread resolved. Thanks.

    What tool are you using in your previous post?
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,422

    Re: [RESOLVED] XMLHTTP GET, passing API parameters

    This is curl with -v option (verbose output) set. Nowadays Win10 comes with curl (and other linux utils) preinstalled.

    If you look carefully the folder it's run in is d:\TEMP\curl-7.69.1_1-win32-mingw so this is not the built-in curl I'm using exactly but an openssl version 7.69 (pre-compiled with mingw) so the output is somewhat different than the built-in one (it's more legible to me).

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width