简体   繁体   English

接收错误415:使用REST API发送GET请求时,媒体类型不受支持

[英]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.

相关问题 “标题”:“不支持的媒体类型”,“状态”:从 python 请求 API 时出现 415 错误 - “title”:“Unsupported Media Type”,“status”:415 error when request API from python 向服务器发送数据时 HTTP 415 不支持的媒体类型客户端 - HTTP 415 Unsupported Media Type client when sending data to server Python GET 请求 - 415 不支持的媒体类型 - Python GET requests - 415 unsupported media type 请求中不支持的媒体类型“text/plain;charset=UTF-8”。 NextJS API 错误与 Django Rest 框架 - Unsupported media type "text/plain;charset=UTF-8" in request. NextJS API error with Django Rest Framework Watson Discovery Service create_collection()api调用返回错误415:不支持的媒体类型 - Watson Discovery service create_collection() api call returns Error 415: Unsupported Media Type requests.exceptions.HTTPError: 415 Client Error Unsupported Media Type when using python zeep - requests.exceptions.HTTPError: 415 Client Error Unsupported Media Type when using python zeep 如何使用请求修复Python中的&#39;415 Unsupported Media Type&#39;错误 - How to fix '415 Unsupported Media Type' error in Python using requests BlueJeans ApiException:415“不支持的媒体类型” - BlueJeans ApiException: 415 “Unsupported Media Type” 415 不支持的媒体 - API 发布 Javascript - Django - 415 UNSUPPORTED MEDIA - API Post Javascript - Django request.post 不支持的媒体类型错误 - Unsupported Media type error for request.post
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM