-1
 Having a  raise JSONDecodeError(errmsg, string, idx)

simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ERROR running the above code its been days now please help still new to python and flask as well.


     from flask import request
        from flask import Flask, jsonify
        import json
        import requests
        import base64
        from icalendar import Calendar, Event
        from datetime import datetime
        from pytz import UTC # timezone
        import os
        
        
        app = Flask(__name__) 
        
        @app.route('/')
        def hello_world():
           return "Webhook"
        
        
        @app.route('/webhook', methods = ['POST'])
        def webhook():
            if request.headers['Content-Type']=='application/json':
               res = request.get_json()
            for product in res['Attachments']:
                encode =  product['Content']
                encoded= encode
                data = base64.b64decode(encoded)
                f = open("guru99.ics","wb")
                f.write(data)
                f.close() 
                cal = Calendar()
                cal.add('prodid', '-//My calendar product//mxm.dk//')
                cal.add('version', '2.0')
                
                g = open('guru99.ics','rb')
                gcal = Calendar.from_ical(g.read())
                for component in gcal.walk():
                    print (component.name)
                g.close()
                print()
                g = open('guru99.ics','rb')
                gcal = Calendar.from_ical(g.read())
                for component in gcal.walk():
                    if component.name == "VEVENT":
                        start = component.get('dtstart')
                        end = component.get('dtend')
                        stamp =component.get('dtstamp')
                        organ = component.get('ORGANIZER')
                        organiser =organ.split(":",1)
                        organiser =  organiser[1]
                        loca =component.get('LOCATION') 
                        location =loca.split(",",1)
                        location = location[0]
                        attend = component.get('ATTENDEE')
                        summary =component.get('summary')
                        duration = end.dt-start.dt
                        print(summary)
                        print(organiser)
                        print(location)
                        print(start.dt)
                        print(end.dt)
                        print(stamp.dt)
                        print(end.dt-start.dt)
                            
                for element in  attend :
                    if element:
                        attendee = list(element.split(":",1))
                        attendee = attendee[1]
                        print(attendee)
                        
                        url = 'https://www.qa.******.com/api/receive_register.php'
                        data = {'organiser': organiser, 'location': location,'attendee': attend,'meeting_name': summary, 'start_date': str(start.dt), 'end_date': str(end.dt), 'timestamp': str(stamp.dt)}
                        
                        
                      
                        response = requests.post(url, json.dumps(data),  headers = {'Content-type': 'application/json'})
                        
                        print(response.text)
                        return response.json()
                   
                        g.close()

 Having a   raise JSONDecodeError(errmsg, string, idx)

simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0) running the above code its been days now please help still new to python and flask as well.

Ausy
  • 31
  • 1
  • 8
  • have you gone through [error](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406) and try to solve the issue mentioned there in your request – sahasrara62 Oct 30 '20 at 11:29
  • Yes I tried everything I could find on google, changing headers content-type but im still getting 406, my server is expecting json, I can't tell which format is being returned here – Ausy Oct 30 '20 at 11:32
  • have you test your api with postman or other same api testing software ?, most probably this error is because of the inner for loop, you need to test your api first inorder to fully sove it – sahasrara62 Oct 30 '20 at 11:37
  • I tested with postman, if I post json data the receiving server is getting it and saving, the problem is I cant seem to get the format of the json data that this function is return, if I try to print the result im getting the 406 error. If I use requestBin I can get a json response. – Ausy Oct 30 '20 at 11:47
  • I am able to get my json response if I change my url "url = 'https://www.qa.******.com/api/receive_register.php'" to a testing pipedream url, but its not working with my server. – Ausy Nov 02 '20 at 07:51
  • you can go through [similar problem](https://stackoverflow.com/questions/14251851/what-is-406-not-acceptable-response-in-http) also i suggest you to return data outside of the for loop not inside – sahasrara62 Nov 02 '20 at 09:32
  • @sahasrara62 Tried returning data outside the loop but i'm still getting the same error – Ausy Nov 02 '20 at 11:23
  • well need to see it in more details, rest regarding the error, i suugest create a sep function or logger to ssee data from the request.json to end and go step by step, see data at each step and try to return that data first in front end or in return, test it out in postman and then proceed to next step in code process and again follow same process – sahasrara62 Nov 02 '20 at 11:35
  • When I print the data using json.dumps(data), its returning proper json which when I send a post request to the server using Postman it works, the problem seems to be on this line" response = requests.post(url, json.dumps(data), headers = {'Content-type': 'application/json'})" its returning "simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" and a 406 response maybe meaning that the data isnt in the right format, so the server is rejecting it, the problem is i'm not able to see the format that requests is returning. – Ausy Nov 02 '20 at 13:03

1 Answers1

0

I found the solution, was to reach my service provider's customer service to whitelist the endpoint to accept calls from my external Api.Mod_Security error. Hope that will someone who might get stuck at some point.

Ausy
  • 31
  • 1
  • 8