Reputation: 177
Background
I am using Python requests to POST data to a google spreadsheet. The request is failing due to an unexpected token "\n". However, to my knowledge, there are no newline characters in the JSON content I am sending. Where are these newline characters coming from? Perhaps I need to define content-length in my headers. Any direction here would be helpful.
Python Code
def appendData(self, sheetName, rowData):
rangeName = sheetName+"!A1"
url = self.baseUrl+"/"+self.spreadsheetId+"/values/"+rangeName+":append?valueInputOption=RAW"
requestBody = {
'values': rowData
}
print("\n\nREQUEST BODY\n\n "+str(requestBody))
try:
response, content = self.service.request(url,
method="POST",
headers=self.headers,
body=str(requestBody))
except:
print("Failed appending data")
print("\n\nRESPONSE\n"+str(response))
print("\n\nCONTENT\n"+str(content))
Output
REQUEST BODY
{'values': [['Timestamp', 'Node 1', '00:13:A2:00:41:04:F1:BC', '', '', 'Node 2', '00:13:A2:00:40:D7:B6:27', '', '', 'Node 3', '00:13:A2:00:40:D7:B6:28', '', '', 'Node 4', '00:13:A2:00:41:04:F1:AB', '', '', 'Node 5', '00:13:A2:00:40:D7:B6:2E', '', ''], ['', 'AD0', 'AD1', 'AD2', 'AD3', 'AD0', 'AD1', 'AD2', 'AD3', 'AD0', 'AD1', 'AD2', 'AD3', 'AD0', 'AD1', 'AD2', 'AD3', 'AD0', 'AD1', 'AD2', 'AD3'], [' '], ['Lower Limit', u'4.9', u'4.9', u'4.9', u'4.9', u'4.640957967', u'4.640957967', u'4.636070381', u'4.636070381', u'0.7797653959', u'0.7797653959', u'0.7797653959', u'0.7797653959', u'-1.1', u'-1.1', u'-1.1', u'-1.1', u'-1.1', u'-1.1', u'-1.1', u'-1.1'], ['Upper Limit', u'5.1', u'5.1', u'5.1', u'5.1', u'5.046236559', u'5.046236559', u'5.041348974', u'5.036461388', u'1.009090909', u'1.009090909', u'1.009090909', u'1.004203324', u'-0.9', u'-0.9', u'-0.9', u'-0.9', u'-0.9', u'-0.9', u'-0.9', u'-0.9'], [' ']]}
RESPONSE
{'status': '400', 'content-length': '437', 'x-xss-protection': '1; mode=block', 'transfer-encoding': 'chunked', 'vary': 'Origin, X-Origin, Referer', 'server': 'ESF', '-content-encoding': 'gzip', 'cache-control': 'private', 'date': 'Wed, 16 Aug 2017 14:58:44 GMT', 'x-frame-options': 'SAMEORIGIN', 'alt-svc': 'quic=":443"; ma=2592000; v="39,38,37,35"', 'content-type': 'application/json; charset=UTF-8'}
CONTENT
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unexpected token.\n'], ['Lower Limit', u'4.9', u'4.9', u'4.\n ^",
"errors": [
{
"message": "Invalid JSON payload received. Unexpected token.\n'], ['Lower Limit', u'4.9', u'4.9', u'4.\n ^",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT"
}
}
Headers
self.headers = {'content-type': 'application/json', 'accept-encoding': 'gzip, deflate', 'accept': 'application/json', 'user-agent': 'google-api-python-client/1.6.2 (gzip)'}
Upvotes: 1
Views: 312
Reputation: 3371
The remote API is not complaining about a newline character, but about the unicode prefixes:
>>> print("Invalid JSON payload received. Unexpected token.\n'], ['Lower Limit', u'4.9', u'4.9', u'4.\n ^")
Invalid JSON payload received. Unexpected token.
'], ['Lower Limit', u'4.9', u'4.9', u'4.
^
To convert a JSON-like dictionary into a JSON string, use json.dumps
:
from json import dumps
body = dumps(requestBody)
Upvotes: 2