+ Reply to Thread
Results 1 to 2 of 2

interacting with site API via http functions query (json) with VBA

  1. #1
    Forum Contributor
    Join Date
    05-17-2016
    Location
    Montreal
    MS-Off Ver
    2013
    Posts
    100

    interacting with site API via http functions query (json) with VBA

    I've managed to interact with a database set up for testing purposes on Wix and I'm able to upload json data to it, albeit crudely.

    The issue is it just keeps inserting new rows/columns and I'm genuinely stump as to how I can get it to interact with the existing fields.

    To give you more context on this tester-database:

    Database name: "zpage_test"
    ID : ID of the whole row (cannot be changed)
    title_t1 : key-field - Reference cell for queries to know what rows are targeted for updates...
    int_t1 : called it as such for testing purposes - Integer should go here
    text_t1 : called it as such for testing purposes - String should go here.

    I'm able to interact with the database and I've tried searching online but most examples are fetch based "GET" or simple "POST" which doesn't help me understand how to specifically interact with the existing data..

    Here's the Backend file code:

    export function post_myFunction(request) {
    let options = {
    "headers": {
    "Content-Type": "application/json"
    }
    };
    // get the request body
    return request.body.text()
    .then( (body) => {
    // insert the item in a collection
    return wixData.insert("zpage_test", JSON.parse(body));
    } )
    .then( (results) => {
    options.body = {
    "inserted": results
    };
    return created(options);
    } )
    // something went wrong
    .catch( (error) => {
    options.body = {
    "error": error
    };
    return serverError(options);
    } );
    }


    '''Here's the VBA PUT code (..with Microsoft Scripting Runtime enabled in VBA references / WinHttp.WinHttpRequest.5.1 reference also works...)


    Sub SendJson()
    Dim objHTTP As Object
    Dim Json As String
    Dim result As String
    Set objHTTP = CreateObject("MSXML2.XMLHTTP")
    Url = "https://www.myfakewebsite.com/_functions/myFunction"

    Json = "{""ID"":""d0f6f64a-e6d8-4cac-9285-d3e51bf3cb53"", ""Title"":""tTitle1"", ""t1"": ""1.5"", ""t2"": ""hello""}"
    objHTTP.Open "PUT", Url, False
    objHTTP.setRequestHeader "Content-type", "application/json"
    objHTTP.Send (Json)
    result = objHTTP.responseText

    'Some simple debugging
    Range("A15").Value = result
    Range("D15").Value = Json
    Set objHTTP = Nothing
    End Sub


    Using the provided example, can someone please show me how to structure a PUT request so I can properly interact with existing data on the database in order to be able to feed it updates..

    Thanks for your time!
    Last edited by Exequiel3k; 06-14-2019 at 05:55 PM.

  2. #2
    Forum Contributor
    Join Date
    05-17-2016
    Location
    Montreal
    MS-Off Ver
    2013
    Posts
    100

    Re: interacting with site API via http functions query (json) with VBA

    I’ve learned that I should be using “update”..
    I’ve updated the code but it still not working :/
    Any help will be much appreciated.

    --------------------------backend---------------------------
    export function get_myFunction(request) {
    let options = {
    "headers": {
    "Content-Type": "application/json"
    }
    };
    // get the request body
    return request.body.text()
    .then( (body) => {

    wixData.get("zpage_test", JSON.parse(body))
    .then( (items) => {
    wixData.update("zpage_test", items);
    } )
    // something went wrong
    .catch( (error) => {
    options.body = {
    "error": error
    };
    return serverError(options);
    } );
    }
    )}
    --------------------------vba/excel---------------------------
    Sub Getjson()
    Dim objHTTP As Object
    Dim Json As String
    Dim result As String
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    Url = "https://www.mywebsite.com/_functions/myFunction"

    Json = "{""ID"":""fc9176c4-0202-4214-af4a-58a2ba3fe6a8"", ""Title"":""bert"", ""t1"": ""1.5"", ""t2"": ""hello""}"
    objHTTP.Open "GET", Url, False
    objHTTP.setRequestHeader "Content-type", "application/json"
    objHTTP.Send (Json)
    result = objHTTP.responseText

    'Some simple debugging
    Range("A15").Value = result
    Range("D15").Value = Json
    Set objHTTP = Nothing
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1