0

I'm receiving this error when trying to decode json:

simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Any help will be appreciated.

views.py:

from django.shortcuts import render
import requests

def home(request):
    response = requests.get('https://dev-api.prime.com/api/v1/hub/login')
    data = response.json()
    return render (request, 'home.html', {
        'email': data['email'],
        'password': data['password']
    })

urls.py:

 path ('home/', views.home, name="home"),

home.html

{% extends 'main.html' %}

{% block content %}
  <h2>API</h2>
  <p>Your email is <strong>{{ email }}</strong>, and password <strong>{{ password }}</strong></p>
{% endblock %}

I tried to send POST request from the terminal:

http POST https://dev-api.prime.com/api/v1/hub/login email="[email protected]" password="asssdps"

and get the response I was looking for:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 866
Content-Type: application/json; charset=UTF-8
Date: Wed, 28 Oct 2020 07:09:45 GMT
Server: nginx/1.12.2
Set-Cookie: _language=811f45dc7836f8b3da4c5d04b177501191c20a3f77a46812f864a3bca7d5d3e1a%3A2%3A%7Bi%3A0%3Bs%3A9%3A%22_language%22%3Bi%3A1%3Bs%3A2%3A%22en%22%3B%7D; path=/; HttpOnly
Set-Cookie: _csrf=5f3fb0e6b529660e0a11a97a3fd3e9a85aa3794d20; path=/; HttpOnly
Vary: Accept
X-Debug-Duration: 816
X-Debug-Link: /debug/default/view?tag=5f991938b8410
X-Debug-Tag: 5f991938b8410
X-Frame-Option: SAMEORIGIN
X-Powered-By: PHP/7.2.18
X-UA-Compatible: IE=Edge,chrome=1
X-XSS-Protection: 1

{
    "data": {
        "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MDM4Njg5ODUsImlzcyI6Imh0dHA6XC9cL2Rldi1hcGkuZGVudGFwcmltZS5jb206ODA5NCIsImF1ZCI6Imh0dHA6XC9cL2Rldi1hcGkuZGVudGFwcmltZS5jb206ODA58",
        "complete": false,
        "country": "AL",
        "date_of_birth": null,
        "email": "[email protected]",
        "first_name": "somename",
        "id": 201,
        "last_name": "somename",
        "phone": "08865444567",
        "role": 10,
        "sf_token": "0014H00002dd",
        "type_language": "en",
        "username": ""
    },
    "status": 200,
    "success": true
}
6
  • response = requests.get('dev-api.prime.com/api/v1/hub/login') Did you make sure this line of code actually return something? Commented Oct 28, 2020 at 7:45
  • @Alviandoh yes, I tried to send post request from the terminal, and it returns the correct response Commented Oct 28, 2020 at 7:46
  • please put your the response too, it might help. Commented Oct 28, 2020 at 7:49
  • @Alviandoh It returns the correct credentials for this user. Like email, password, phone, access token and etc. I'm not sure how this can help. Pretty long json response. Commented Oct 28, 2020 at 7:56
  • I'm highly certain this error is caused by no response was given from the url. Did you make sure that 1. you use correct method (POST/GET) 2. no typo in URL? Commented Oct 28, 2020 at 8:01

1 Answer 1

1

This is your request

http POST https://dev-api.prime.com/api/v1/hub/login email="[email protected]" password="asssdps"

While in code,

response = requests.get('https://dev-api.prime.com/api/v1/hub/login')

They are different. In the first request you used "POST" method, with email and password alongside it. While in your code, you simply ask for "GET" method.

Change request code to something like this, and it should be receiving the correct response.

response = requests.post('https://dev-api.prime.com/api/v1/hub/login', json={"email":"[email protected]", "password":"asssdps"})

and Since the response you want is something like this

{
    "data": {
        ...
    },
    "status": 200,
    "success": true
}

The response will return dictionary. And you only want the data. You need to access the value inside "data" key. So change this,

data = response.json()

into,

data = response.json()['data']
Sign up to request clarification or add additional context in comments.

3 Comments

I got KeyError at /home/ 'email'. What is this for? For my home func in views.py.
ah also I forgot about that, I'll be addressing that one in my edit
@pythondeveloper should be addressed with the newest edit with some explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.