Search code examples
restgoogle-sheetsfantom

PUT Request not happening at all in Fantom


I am having some trouble with PUT requests to the google sheets api. I have this code

    spreadsheet_inputer := WebClient(`$google_sheet_URI_cells/R3C6?access_token=$accesstoken`)
    xml_test := XDoc{
    XElem("entry")
       {
         addAttr("xmlns","http://www.w3.org/2005/Atom")
         addAttr("xmlns:gs","http://schemas.google.com/spreadsheets/2006")
         XElem("id") { XText("https://spreadsheets.google.com/feeds/cells/$spreadsheet_id/1/private/full/R3C6?access_token=$accesstoken"), },
         XElem("link") { addAttr("rel","edit");addAttr("type","application/atom+xml");addAttr("href","https://spreadsheets.google.com/feeds/cells/$spreadsheet_id/1/private/full/R3C6?access_token=$accesstoken"); },
         XElem("gs:cell") { addAttr("row","3");addAttr("col","6");addAttr("inputValue","testing 123"); },
       },
    }

    spreadsheet_inputer.reqHeaders["If-match"] = "*"
    spreadsheet_inputer.reqHeaders["Content-Type"] = "application/atom+xml"
    spreadsheet_inputer.reqMethod = "PUT"
    spreadsheet_inputer.writeReq
    spreadsheet_inputer.reqOut.writeXml(xml_test.writeToStr).close
    echo(spreadsheet_inputer.resStr)

Right now it returns

sys::IOErr: No input stream for response 0

at the echo statement.

I have all the necessary data (at least i'm pretty sure) and it works here https://developers.google.com/oauthplayground/

Just to note, it does not accurately update the calendars.

EDIT: I had it return the response code and it was a 0, any pointers on what this means from the google sheets api? Or the fantom webclient?


Solution

  • WebClient.resCode is a non-nullable Int so it is 0 by default hence the problem would be either the request not being sent or the response not being read.

    As you are obviously writing the request, the problem should the latter. Try calling WebClient.readRes() before resStr.

    This readRes()

    Read the response status line and response headers. This method may be called after the request has been written via writeReq and reqOut. Once this method completes the response status and headers are available. If there is a response body, it is available for reading via resIn. Throw IOErr if there is a network or protocol error. Return this.

    Try this:

    echo(spreadsheet_inputer.readRes.resStr)
    

    I suspect the following line will also cause you problems:

    spreadsheet_inputer.reqOut.writeXml(xml_test.writeToStr).close
    

    becasue writeXml() escapes the string to be XML safe, whereas you'll want to just print the string. Try this:

    spreadsheet_inputer.reqOut.writeChars(xml_test.writeToStr).close