question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: error message "JSON parse error..." after REST-query to add-to-cart?format=json

See original GitHub issue

environment: OS xUbuntu 18.04.4; Python 3.6.9; Django 3.0.8; django-shop 1.2.2 (installed according to instructions https://django-shop.readthedocs.io/en/latest/tutorial/intro.html) STR:

  1. get cookie-values csrftoken and sessionid adding 1 product to cart in browser and parsing them from headers of corresponding http-request
  2. execute Python-code (adding 1 products unit to existing cart according to instruction 17.2.3.3. Add Product to Cart)
import httplib2
from urllib.parse import urlencode
h = httplib2.Http('.cache')
data = {
  "quantity": 1,
  "product": 15,
  "product_code": "1004"
}
data = urlencode(data)
response, content = h.request('http://localhost:8000/catalog/smart-cards/sdxc-card-64gb/add-to-cart?format=json','POST',data,headers={'Content-Type':'application/json','accept':'application/json','Cookie':'csrftoken=3hoZkpJNer2kSNaDgO3nOa8r2fLNl6KiwtMJDCW5pbDN9jELORM2dDIYCnE8wwew;sessionid=2k0t3edvimyud5acdpvt71e5dda7jod7'})
print("response.status  -  {0}".format(response.status))
print("response.items()  -  {0}".format(response.items()))
print("content  -  {0}".format(content))

AR: content - b’{“detail”:“JSON parse error - Expecting value: line 1 column 1 (char 0)”}’ ER: understandable error message in the response body

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jriefcommented, Jul 29, 2020

as far as I know, JSON data must be transferred (and will be received) as byte-stream, not string. Maybe you have to check this as well.

1reaction
jriefcommented, Jul 29, 2020

before sending a dict as JSON, you have to stringify it.

...
import json
...
data = {
  "quantity": 1,
  "product": 15,
  "product_code": "1004"
}
data = json.dumps(data)
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
Read more >
Error while parsing JSON Response in REST API Post call
When I invoke the same REST method through the UI, it is raising a parsing error. I switched to returning a JSON String...
Read more >
JSON.parse error on a seemingly valid JSON [duplicate]
The error is a result of data being an object, not JSON. You don't need to parse anything; it is already a JavaScript...
Read more >
In rest message getting {"detail":"JSON parse error - ServiceNow
Hi, I am getting {"detail":"JSON parse error - Invalid \\escape: line 11 column 31 (char 359)\nPossible cause: trailing comma."}
Read more >
JSON parse error when displaying validation error in tree mode
Using schema and json provided below, I get a JSON parse error, and a broken tree view. Failed to parse JSON schema: SyntaxError:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found