+ Reply to Thread
Results 1 to 3 of 3

Update database via PUT http functions (json) with Excel/VBA

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

    Update database via PUT http functions (json) with Excel/VBA

    I've managed to interact with a database set up for testing purposes on Wix. I'm able to upload json data to it and I can see it in the LIVE database, 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 existing fields.

    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)

    Sub SendJson()
    Dim objHTTP As Object
    Dim Json As String
    Dim result As String
    Set objHTTP = CreateObject("MSXML2.XMLHTTP")

    Url = "https://www.mywebsite.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

    could some insight be provided as to whats failing here? I'd really appreciate your help!
    Last edited by Exequiel3k; 06-26-2019 at 10:21 AM.

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

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

    ---UPDATE TO THE INQUIRY!---

    I'm still trying to get it to work.. Here’s the updated code:

    ---WIX BACKEND---

    import {created, serverError, ok, notFound} from 'wix-http-functions';
    import wixData from 'wix-data';
    export function put_myFunction(request) {
    let options = {
    "headers": {
    "Content-Type": "application/json"
    }
    };

    // get the request body
    return request.body.text()
    .then( (body) => {

    // update the item in a collection
    return wixData.update("zTest_DevDaily", JSON.parse(body));
    } )
    .then( (results) => {
    options.body = {
    "inserted": results
    };
    return ok(options);
    } )

    // something went wrong
    .catch( (error) => {
    options.body = {
    "error": error
    };
    return serverError(options);
    } );
    }

    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("zTest_DevDaily", JSON.parse(body));
    } )
    .then( (results) => {
    options.body = {
    "inserted": results
    };
    return created(options);
    } )

    // something went wrong
    .catch( (error) => {
    options.body = {
    "error": error
    };
    return serverError(options);
    } );
    }

    ---EXCEL / VBA---

    Sub SendJson()
    Dim objHTTP As Object
    Dim text As String
    Dim jSON As Object
    Dim result As String

    Dim rng As Range, items As New Collection, myitem As New Dictionary, subitem As New Dictionary, i As Integer, cell As Variant

    Set objHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")

    Set rng = Range("A2:A2")
    'Set rng = Sheets(1).Range("A2").End(xlDown)) 'dynamic range
    i = 0
    For Each cell In rng
    Debug.Print (cell.Value)
    myitem("Title") = cell.Value
    myitem("ID") = cell.Offset(0, 1).Value
    myitem("Owner") = cell.Offset(0, 2).Value
    myitem("Created Date") = cell.Offset(0, 3).Value
    myitem("Updated Date") = cell.Offset(0, 4).Value
    myitem("VerDate") = cell.Offset(0, 5).Value
    items.Add myitem
    Set myitem = Nothing
    Set subitem = Nothing
    i = i + 1
    Next

    URL = "https://www.MYWEBSITE.com/_functions/myFunction"
    objHTTP.Open "PUT", URL, False
    objHTTP.setRequestHeader "Content-type", "application/json"
    objHTTP.Send ConvertToJson(items, Whitespace:=2)
    result = objHTTP.ResponseText

    'Some simple debugging
    Range("A25").Value = result
    Range("A26").Value = ConvertToJson(items, Whitespace:=2)
    Set objHTTP = Nothing
    End Sub
    --

    I'm using EXCEL/JSON convertor: https://github.com/VBA-tools/VBA-JSON

    I've copied the database in excel to then apply the conversion and this is what a json entry value looks like:

    [
    {
    "Title": "test1",
    "ID": "a4ef53df-903c-44e5-a014-f8ae1d937b5f",
    "Created Date": "2019-06-21T17:43:38Z",
    "Updated Date": "2019-06-23T19:23:51Z",
    "VerDate": 3
    }
    ]

    POST works
    PUT generates the following error (invalid token???):

    "{"error":"name":"Error","errorGroup":"User","code":"WD_VALIDATION_ERROR"}}"

    That said, I'd really appreciate anybOdy helping me crack this code as my website fundamentally depends on this working in the longrun.
    Last edited by Exequiel3k; 06-26-2019 at 10:22 AM.

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

    Re: Update database via PUT http functions (json) with Excel/VBA

    I've updated the previous post to reflect where I'm at..

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Advanced JSON Data Query with Web API (URL parts?)
    By SheToastsMe in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-13-2017, 11:01 PM
  2. Query Editor - Json formating
    By TheVulcan in forum Excel General
    Replies: 0
    Last Post: 07-03-2017, 02:30 AM
  3. Replies: 2
    Last Post: 08-13-2014, 12:10 PM
  4. Web Query Question About http address.
    By Joemandingo in forum Excel General
    Replies: 0
    Last Post: 02-20-2014, 01:45 PM
  5. Replies: 0
    Last Post: 07-25-2013, 04:23 PM
  6. Http Request Repsonse Query
    By Killavirus in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-25-2010, 07:38 PM
  7. Looking for a site with functions that substitute the ATP functions
    By Franz Verga in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-23-2006, 11:35 PM

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