我已经创建了一些与Exact Online进行通信的python代码,并希望通过使用带有urllib2的python发送GET以及POST请求。
现在当我尝试发送一个简单的GET请求时,包括'Accept:application / json'之类的标题,当我在测试应用程序(SOAP-UI)中尝试它时,它会起作用
我的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
结果是
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
我已尝试使用Content-Type和Accept以及两者都没有。所有人都给出了同样的响应415。 当我尝试只使用
时request.add_header("Content-Type", "application/json")
或
request.add_header("Content-Type", "application/atom+xml")
我收到了以下回复:
urllib2.HTTPError: HTTP Error 400: Bad Request
我认为这可能是造成这一切的原因。
有人知道如何解决这个问题吗?
也许值得一提的是,当我用另一个应用程序测试时,响应有Content-Type: application/atom+xml;charset=utf-8
答案 0 :(得分:0)
我发现有些网络服务器要求您发送User-Agent
标头才能正常工作。
尝试附加:
request.add_header("User-Agent", "what u like")