sending a JSON string
Andre Alves Garzia
andre at andregarzia.com
Tue Oct 16 19:10:05 EDT 2018
Douglas,
I am pasting some code I've built here that should do what you want but
when I try it, it returns a 401 error which is an authentication error.
On that manual page you linked there is a notice about API keys and
Channel keys, so maybe the key on this email is not the correct one.
This is the code (and yes, it is dead ugly):
*on*mouseup
*local*tHeaders, tResult
*put*emptyintofld"output"
*put*"Content-Type: application/x-www-form-urlencoded"intotHeaders
*put*libURLFormData("api_key", "EQKTUQQKVH83D1RE") intotFormData
*put*tsNetCustomUpload("1",
"https://api.thingspeak.com/channels/564256/feeds.json", "DELETE",
tHeaders, "transferComplete", tFormData) intotResult
*end*mouseup
*on*transferComplete pID, pResult, pBytes, pCurlCode
*local*tData, tHeaders, tResult, rError
*if* pCurlCode isnot0*then*
*put*tsNetRetrError(pID) & crafterfld"output"
*end* *if*
*put*tsNetRetrData(pID, tError) intotData
*put*"Server returned:"& tData & crafterfld"output"
*put*"Headers"&& tsNetRetrSentHeaders("1", rError) & crafterfld"output"
*put*tResult & cr& tHeaders & cr& pResult & cr& tData afterfld"output"
tsNetCloseConn pID
*end*transferComplete
On 10/16/2018 9:12 PM, Douglas Ruisaard via use-livecode wrote:
> I'll start by apologizing for my abysmal familiarity with HTTP, JSON, and URL's ... I'm hoping someone will have pity and give me a hand!
>
> I am trying to send a "command" to a ThingSpeak channel, which is very successfully receiving data from a simple little ESP8266 module. Periodically, I need to clear the data from this channel. I want to do so from LC.. NOT the ESP8266. I just can't seem to "coordinate" the instructions from the ThingSpeak site to DELETE this data (not the channel, just the data within) with an LC function / code.
>
> The link to the ThingSpeak page I am referring to is:
> https://www.mathworks.com/help/thingspeak/clearchannel.html?searchHighlight=delete%20channel%20data&s_tid=doc_srchtitle#d120e16923
>
> but in essence, I interpret the content to assert the following:
>
> Access the following URL: "https://api.thingspeak.com/channels/564256/feeds.json" where "564256" is the channel I wish to clear.
>
> Send the JSON "command": "DELETE https://api.thingspeak.com/channels/564256/feeds.json
> api_key= EQKTUQQKVH83D1RE"
>
> where "EQKTUQQKVH83D1RE" is my account api-key for writing to the ThingSpeak channel
>
> if successful, ThingSpeak responds with: "[]" ... an empty JSON array
>
>
> In LC, I've tried the following (and several variations):
>
> on mouseUp
> put empty into field "Output"
> put "https://api.thingspeak.com/channels/564256/feeds.json" into tUrl
> put "DELETE https://api.thingspeak.com/channels/564256/feeds.xml" & return & "api_key=EQKTUQQKVH83D1RE" into JsonCmd
>
> -- Send a HTTP DELETE request
> put tsNetCustomSync(tUrl, JsonCmd, tHeader, tResponseHeaders, tResult, tBytes) into tData
>
> -- If this is not the case, we will inform the user
> put tResult into field "Output"
>
> end mouseUp
>
> I get a tResult of 0 (zero) ... but the data is NOT cleared.
>
> I have also tried the following:
>
> on mouseUp
> put empty into field "Output"
> put "https://api.thingspeak.com/channels/564256/feeds.json?api_key=EQKTUQQKVH83D1RE" into tUrl
> put "DELETE https://api.thingspeak.com/channels/564256/feeds.json" & return & "api_key=EQKTUQQKVH83D1RE" into tMessage["text"]
>
> put ArrayToJSON(tMessage) into tMessage
>
> set the httpHeaders to "Content-type: application/json"
>
> post tMessage to url tUrl
>
> put it into field "Output"
>
> end mouseUp
>
> But that only "creates" a new entry in the channel with all of the data fields empty.
>
> I'm sure it's the way in which I am trying to send the "DELETE" command but I just can't see how to do it correctly.
>
> I'd be grateful for any assistance.
>
>
> Douglas Ruisaard
> Trilogy Software
> (250) 573-3935
>
>
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list