Bulk upload issue - HTTP Error code 502

8 Ansichten (letzte 30 Tage)
Johannes Kuder
Johannes Kuder am 6 Jan. 2022
Beantwortet: Vinod am 6 Jan. 2022
Dear community,
I'm trying to bulk upload data from my IoT server to ThingSpeak through a Python script. I get HTTP Error code 502 as response, which obviously means "Bad Gateway" - "The server received an invalid response from the upstream server. Check your network connection and try again."
No idea how to fix this. Can anybody please help?
import requests
import csv
import json
indices = {
"created_at" : 0,
"field1" : 1,
"field2" : 2,
"field3" : 4,
"field4" : 6,
"field5" : 7,
"field6" : 8,
"field7" : 9,
"field8" : 10,
}
r = requests.get("https://[MyServer]/[MyFile.csv]")
rows = r.text.splitlines()
reader = csv.reader(rows)
parsed_csv = list(reader)
objects = []
for row in parsed_csv:
new_object = {}
for key, value in zip(indices.keys(), indices.values()):
new_value = row[value]
new_object[key] = new_value
objects.append(new_object)
body = {
"write_api_key" : "MyWriteApiKey",
"Content-Type" : "application/json",
"updates" : objects,
}
response = requests.post("https://api.thingspeak.com/channels/[MyChannel]/bulk_update.json", data=json.dumps(body))
print(response.status_code)

Antworten (2)

Yongjian Feng
Yongjian Feng am 6 Jan. 2022
You are using python code, but this is a matlab forum, right?
In general, three suggestions:
  1. Try curl. This can tell if there is something wrong in your python code
  2. Check the document of the API of thingspeak.com. See if there is any example you can use. Check if the example works.
  3. Find the forum of thingspeak. Ask there.

Vinod
Vinod am 6 Jan. 2022
@Johannes Kuder: Thank you for posting your question on the ThingSpeak Community. Like @Yongjian Feng says, you should try your request with CURL or POSTMAN first.
One thing I notice is that your code does not show any delays. How often are you making these bulk_update requests? Perhaps you're getting a 502 because you are exceeding the rate limit? Please refer to the ThingSpeak documentation for the maximum frequency of bulk_update limits and incorporate the necessary delays in your code. Note that knowingly abusing the rate limits is considered abuse of service and I'd refer you to terms of service of MathWorks Account so you don't run afoul of it.

Communitys

Weitere Antworten in  ThingSpeak Community

Kategorien

Mehr zu Read Data from Channel finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by