[英]Receiving Error 415 : Unsupported media type when sending GET request with a REST API
I've created some python code to communicate with Exact Online and want to send GET aswell as POST requests by using python with urllib2. 我创建了一些python代码以与Exact Online通讯,并希望通过将python与urllib2一起使用来发送GET和POST请求。
Now when i try to send a simple GET request including headers like 'Accept : application/json', which works when I tried it in a test application(SOAP-UI) 现在,当我尝试发送包含诸如“ Accept:application / json”之类的标头的简单GET请求时,当我在测试应用程序(SOAP-UI)中对其进行尝试时,该请求就可以工作
My python code is as followed : 我的python代码如下:
import urllib2, urllib, json, requests, base64
data = urllib.urlencode({
})
request = urllib2.Request(
url="https://start.exactonline.nl/api/v1/"+divis+"/salesorder/SalesOrderLines?access_token=" + access,
data=data)
base64string = base64.encodestring('%s:%s' % (user,password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
request.add_header("Content-Type", "application/json+xml")
request.add_header("Accept", "application/json+xml")
request= urllib2.urlopen(request)
print request_open
response = request_open.read()
print response
Which results in 导致
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 70, in <module>
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 45, in retrieve_tokens
File "C:\Python27\Lib\urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\Lib\urllib2.py", line 410, in open
response = meth(req, response)
File "C:\Python27\Lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\Lib\urllib2.py", line 448, in error
return self._call_chain(*args)
File "C:\Python27\Lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "C:\Python27\Lib\urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 415: Unsupported Media Type
I have tried this with both Content-Type and Accept aswell as both without the other. 我已经尝试了Content-Type和Accept以及两者都没有。 And all gave the same 415 response.
所有人都给出了相同的415响应。 When i tried using only
当我尝试仅使用
request.add_header("Content-Type", "application/json")
or 要么
request.add_header("Content-Type", "application/atom+xml")
I got the following response : 我得到以下回应:
urllib2.HTTPError: HTTP Error 400: Bad Request
I think this might be the cause of all this. 我认为这可能是所有这些的原因。
Anybody have an idea how to fix this? 有人知道如何解决此问题吗?
Maybe worth mentioning that when i tested this with another application, the response had Content-Type: application/atom+xml;charset=utf-8
也许值得一提的是,当我使用另一个应用程序进行测试时,响应的
Content-Type: application/atom+xml;charset=utf-8
I have found some web servers require you to send a User-Agent
header in order to to work. 我发现有些Web服务器要求您发送
User-Agent
标头才能正常工作。
Try appending this: 尝试附加以下内容:
request.add_header("User-Agent", "what u like")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.