1

I have done this code to get an output form the web page of http://www.yurticikargo.com/bilgi-servisleri/Sayfalar/en-yakin-sube.aspx but I receive 500 error. Could not understand why

import requests
import json


class Yurtici(object):
    baseUrl = 'http://www.yurticikargo.com/'
    ajaxRoot1 = '_layouts/ArikanliHolding.YurticiKargo.WebSite/ajaxproxy-sswservices.aspx/'
    ajaxRoot2 = '_layouts/ArikanliHolding.YurticiKargo.WebSite/ajaxproxy-unitservices.aspx/GetUnit'

    getTown = 'GetTownByCity'

    urlGetTown = baseUrl + ajaxRoot1 + getTown
    urlGetUnit = baseUrl + ajaxRoot2  
    headers = {'content-type': 'application/json','encoding':'utf-8'}

    def __init__(self):
        pass

    def ilceler(self, plaka=34): # Default testing value
        payload = {'cityId':plaka}
        url = self.urlGetTown
        r = requests.post(url, data=json.dumps(payload), headers=self.headers)
        return r.json() # OK


    def subeler(self, ilceNo=3401): # Default testing value
        # 5902 Çerkezköy 
        payload=  {'cityID':34,'townID':ilceNo,'unitOnDutyFlag':'null','closestFlag':0}
        url = self.urlGetUnit
        headers = {'content-type': 'application/json','encoding':'utf-8'}
        r = requests.post(url, data=json.dumps(payload), headers=headers)
        return r.json()
        print  r.status_code, r.raw.read()

if __name__ == '__main__':        
    a = Yurtici()
   #print a.ilceler(34) # OK
    print a.subeler()   # NOT OK !!!

1 Answer 1

1

Just on a hunch, I found a problem with the 'unitOnDutyFlag' value. When encoding a python dict to json, if you want null in the output, you should have a value of None in the dict.

Try changing the line:

payload = {'cityID':34,'townID':ilceNo,'unitOnDutyFlag':'null','closestFlag':0}

to:

payload = {'cityID':34,'townID':ilceNo,'unitOnDutyFlag': None,'closestFlag':0}

Example:

# nulljson.py
import json

data = {
    'foo': 'null',
    'bar': None
}

print json.dumps(data)

Output:

$ python nulljson.py
{"foo": "null", "bar": null}

Note that the value of "foo" is the string "null", not the true null value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.