0

I am attempting to improve my python by working on a side-project, just interacting with websites to look at avaialble golf tee times. Zero intention/plans to do anything commercial, this is just a semi-practical way to learn more about the requests library and web site/api interaction.

If I use the same URL, headers, and JSON payload, I get back two different results. In both cases I get status 200, but using postman, the result body is json data of available tee times for a given date and golf course.

If I post the same request using python, I get back html/website data and not the JSON. I've attempted as much debugging as I can, like verifying the headers and body are set the same for both requests. Also no authorization is being used in either postman or python. What else can I check or how can I test this further?

Im including code of the API/website request, as well as screenshots from Postman.

for course, id in golfback_courses.items():
  gc_url = "https://api.golfback.com/api/v1/courses/" + id + "/date/" + golf_date + "/teetimes?utm_source=drumm_farm_website&utm_medium=button&utm_campaign=tee_times"
  headers = {"Content-Type": "application/json; charset=utf-8"}
  payload = json.dumps({'sessionId': None})
  print(f"Tee Times for {course} on {golf_date}: ")
  response = requests.post(gc_url, headers=headers, json=payload, allow_redirects=True)
  
  #debugging prints and checking status codes
  print(response.status_code)
  print(dump.dump_all(response).decode("utf-8"))
  html_content = response.content
  print(type(html_content))
  
  soup = BeautifulSoup(html_content, "html.parser")
  #additional json/text manipulation and print logic once I verify I have json object

I'm also attaching a screenshot of the request and body response from postman. Im showing the headers, which for the exception of the Postman-Token, I verified all are the same on the python side, or, disabled them in postman and verified I still get the same response. The body is simply raw JSON passing in a null sessionId, i.e. {"sessionId": null}. I verified via json.dumps that the {'sessionId': None} in python encodes to sessionId: null.

enter image description here

Any further troubleshooting help is appreciated.

15
  • requests has different header User-Agent than postman or curl and sometimes it makes difference. Commented Sep 16 at 19:45
  • postaman has function to generate code in different languages - there is also Python with requests. You may check if it will generate the same code as you created. Commented Sep 16 at 19:47
  • you can use directly dictionary with json={'sessionId': None} without json.dumps() Commented Sep 16 at 19:49
  • @furas thank you for the suggestions. I did disable the User Agent header in postman to see if that would have any affect and it did not, I will look into the code generator. Commented Sep 16 at 19:49
  • 1
    other idea: send requests from postman and Python to httpbin.org/post and it will send you back all your headers, cookies, etc. and you can compare if both sends the same values. You may also try to use local proxy server like Charlies or mitmproxy to see what send postman (if you use local application) and Python Commented Sep 16 at 20:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.