Bulk-Write CSV Data in python | Raspberry Pi | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Bulk-Write CSV Data in python
No permission to create posts
April 22, 2019
5:23 am
Avatar
oalonso
New Member
Members
Forum Posts: 2
Member Since:
April 22, 2019
sp_UserOfflineSmall Offline

Hi,

I am trying to bulk-write the data from the example : https://es.mathworks.com/help/thingspeak/bulkwritecsvdata.html#mw_9108a4c1-a09f-4a9c-94fe-8e6dde3634ea_seealso

However, I am always generating an error code (400 - Bad request) and I don't know why. Can anyone, watching my simple code, help me ? Thanks in advance !

import requests
url = "https://api.thingspeak.com/channels/"+channelID+"/bulk_update.csv" #I use my channelID

data = "write_api_key=XXXXXXXXXXXXXXXX&time_format=absolute&updates=2018-01-30T10%3A26%3A2-0500%2C2%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%7C2018-01-30+10%3A26%3A23-0500%2C2%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%7C2018-01-31+10%3A27%3A2-0500%2C16%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%7C2018-01-31+11%3A26%3A32-0500%2C2%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%7C2018-01-31+11%3A27%3A22-0500%2C164%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C"
# I use my api key instead of Xs
requestHeaders = {"User-Agent":"mw.doc.bulk-update (Raspberry Pi)","Content-Type":"application/x-www-form-urlencoded","Content-Length":str(len(data))}

r = requests.post(url,data=data,headers=requestHeaders)
print(r.status_code)

############################
#I have the same results with urllib2:
############################
import urllib2 as ul

req = ul.Request(url = url)
req.add_header("User-Agent","mw.doc.bulk-update (Raspberry Pi)")
req.add_header("Content-Type","application/x-www-form-urlencoded")
req.add_header("Content-Length",str(len(data)))

req.add_data(data) # Add the data to the request

try:
response = ul.urlopen(req) # Make the request
print response.getcode() # A 202 indicates that the server has accepted the request
except ul.HTTPError as e:
print e.code

April 22, 2019
11:37 am
Avatar
oalonso
New Member
Members
Forum Posts: 2
Member Since:
April 22, 2019
sp_UserOfflineSmall Offline

I have finally solved, the problem was that I was not encoding the message properly

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
23 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

chen_jin_hui@yahoo.com, Vadisa, arva, zarin, quicklearner7, ssh47k

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457