16

I am getting 415 error while posting data to server. This is my code how can i solve this problem. Thanks in advance!

import requests
import json
from requests.auth import HTTPBasicAuth
#headers = {'content-type':'application/javascript'}
#headers={'content-type':'application/json', 'Accept':'application/json'}
url = 'http://IPadress/kaaAdmin/rest/api/sendNotification'
data = {"name": "Value"}
r = requests.post(url, auth=HTTPBasicAuth('shany.ka', 'shanky1213'),json=data)
print(r.status_code)
Mitra Mishra
  • 161
  • 1
  • 1
  • 4

3 Answers3

34

According to MDN Web Docs,

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.

The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly.

In your case, I think you've missed the headers. Uncommenting

headers={
    'Content-type':'application/json', 
    'Accept':'application/json'
}

and including headers in your POST request:

r = requests.post(
    url, 
    auth=HTTPBasicAuth('shany.ka', 'shanky1213'),
    json=data,
    headers=headers
)

should do the trick


import requests
import json
from requests.auth import HTTPBasicAuth


headers = {
    'Content-type':'application/json', 
    'Accept':'application/json'
}
url = 'http://IPadress/kaaAdmin/rest/api/sendNotification'
data = {"name": "Value"}

r = requests.post(
    url, 
    auth=HTTPBasicAuth('shany.ka', 'shanky1213'), 
    json=data, 
    headers=headers
)
print(r.status_code)
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
  • 2
    Being a newbie to Python, I've been confused with json x data parameter difference, i kept receiving 415 while using data parameter to send the body. Using json parameter solves the issue, and you don't even need to send the content type header. – Diego Victor de Jesus Apr 26 '21 at 03:56
  • 1
    I get the same error and post it as a new question here: https://stackoverflow.com/questions/67936078/python-request-gives-415-error-while-post-data-unless-data-keyvalue and the solution is the same too. i have to add `data={"key,"value"}` to make it work, even though `data` is an optional keyword according to the docs. – D.L Jun 11 '21 at 11:22
  • 1
    I've also encountered the same issue. Thank you for help. – Soren V. Raben Jan 12 '22 at 10:23
  • Hi @Giorgos, can you please tell how can I access the "data" sent in "json=data". – NeedToCodeAgain Aug 30 '22 at 05:03
4

As a workaround, try hitting your api using Postman. When you can successfully hit the api in postman, generate python code in postman (button is present in the top right corner). You can copy the code in your python project.

Aditya
  • 84
  • 3
  • 1
    Postman actually gives you good code. I ran into the same issue but I followed this, then got my issue sorted. Here's a link to see how to get code from postman https://www.youtube.com/watch?v=4XxcBI2uViA – Chris Claude Apr 23 '20 at 10:46
1

Another possible cause is using requests.post when you should be using requests.get or vice versa. I doubt that this is a common problem, but in my case a server that was happy to accept an HTTP GET for a search rejects it with a 415 when HTTP POST is used instead. (Yet another site required that a search be requested using HTTP POST. It was reusing that code that caused my problem.)