Bulk upload issue - HTTP Error code 502

조회 수: 1 (최근 30일)
Johannes Kuder
Johannes Kuder 2022년 1월 6일
댓글: Christopher Stapels 2022년 1월 9일
Obviously I asked my question in the wrong forum before. Hope this is the right one.
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)
Of course I have checked the document of the API of thingspeak.com, and I don't think that I will exceed the rate limit, since this upload is a one-time call, and the data amount in the CSV file is small enough.
Thanks Johannes
  댓글 수: 3
Johannes Kuder
Johannes Kuder 2022년 1월 6일
Hi CHristopher, please find the csv file in the attachment
Christopher Stapels
Christopher Stapels 2022년 1월 9일
Im sorry, I didnt read far enough into your code. You are using the json endpoint for bulk. We also have a csv endpoint which Is how I got confuesd. You are reading the csv file from somewhere else to create the JSON request.
The bulk syntax can be tricky, though I wouldnt expect a 502 from bad syntax. Just to be sure, can you hard code a request of the proper format instead of creating it dynamically from the local file? The endpoint might be getting confused if you send extra material in the header.

댓글을 달려면 로그인하십시오.

답변 (0개)

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Read Data from Channel에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by