-1

I am doing a straight forward request as follows.

import requests

def user_transactions():
    url = 'https://webapi.coinfloor.co.uk/v2/bist/XBT/GBP/user_transactions/'
    
    data = {'key':'value'}
    r = requests.post(url, data=data, auth=("some_username", "some_password") )
    print(r.status_code)
    print(r.text)
    return

Even though data= is optional in the documents. https://www.w3schools.com/python/ref_requests_post.asp

  1. If i comment out the data variable then the routine returns a status_code=415 error.
  2. If i include in the data variable then the routine returns a status_code=200 success.

I have tried to look this up, for example here: Python request gives 415 error while post data , but with no answer.

The question is: Why is it the case that [1] fails but [2] works ?

D.L
  • 4,339
  • 5
  • 22
  • 45
  • 1
    W3schools is not "the docs" and the data argument is required. [The real docs](https://docs.python-requests.org/en/v2.7.0/user/quickstart/#response-content) – OneMadGypsy Jun 11 '21 at 11:33
  • I actually looked at "the real docs" too, but it wasn't clear hence the w3schools link which had a nice clear table (albeit what you asserted is incorrect) and appreciate. – D.L Jun 11 '21 at 11:55
  • What I asserted was incorrect from the python end. However the python end matters very little when you are relying on the server end to respond to your blank request, which it probably will not like. You have to consider all requirements, not just the ones that your language does or does not have. So, sure, my assertion was incorrect, except you still have to use it. – OneMadGypsy Jun 11 '21 at 20:17

2 Answers2

1

Yes, data is optional on the python side. The requests library will happily send a empty request to the server, as you can see. If the argument was not optional, the program would crash before sending a request so there would be no status code.

However, the server needs to be able to process the request. If it does not like what you sent for whatever reason, it might send back a 4xx status code, or otherwise not do what you expect.

In this case, it throws an error that the data is in invalid format. How can a empty request be in invalid format? Because the format is specified in a header. If you supply a data argumet requests will send data in urlencoded format, and specify in the header what format the data is in. If the data is empty, the request will be empty but the header will still be there. This site apparently requires the header to specify a data format it knows.

You can solve this in two ways, giving an empty object:

r = requests.post(url, data={}, auth=("some_username", "some_password") )

Or by explicitly specifying the header:

r = requests.post(url, auth=(...), headers={'Content-Type': 'application/x-www-form-urlencoded'})

Side note: You should not be using W3Schools as a source. It is frequently inaccurate and often recommends bad practices.

mousetail
  • 7,009
  • 4
  • 25
  • 45
  • I marked this as correct as it is the most complete answer. For the record, setting `data={}` does not work, whereas setting `headers={'Content-Type': 'application/x-www-form-urlencoded'}` does work. my follow up question is how do i know which to use without having come here ? – D.L Jun 11 '21 at 12:01
  • You should be able to find this in the API documentation of whatever API you used, or in this case simply by reading the error message – mousetail Jun 11 '21 at 12:02
0

I think you are mistaking the documentation of the requests.post function signature with API documentation. It is saying that data is a keyword argument, not that the API optionally takes data.

It depends on the API endpoint you are trying to use. That endpoint must require data to be sent with the request. If you look at the documentation for the API you are using, it will mention what needs to be sent for a valid request.

Robert Kearns
  • 1,631
  • 1
  • 8
  • 15
  • hi, thanks. it was pointed out that w3schools was incorrect and that the docs are here: https://docs.python-requests.org/en/master/user/quickstart/ . – D.L Jun 11 '21 at 12:12