json 什么是 HTTP 中的“406-Not Acceptable Response”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14251851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is "406-Not Acceptable Response" in HTTP?
提问by Cyber
In my Ruby on Rails application I tried to upload an image through the POSTMAN RESTclient in Base64format. When I POST the image I am getting a 406 Not Acceptable Response. When I checked my database, the image was there and was successfully saved.
在我的 Ruby on Rails 应用程序中,我尝试通过 POSTMAN REST客户端以Base64格式上传图像。当我发布图像时,我收到406 Not Acceptable Response。当我检查我的数据库时,图像在那里并成功保存。
What is the reason for this error, is there anything I need to specify in my header?
这个错误的原因是什么,我需要在我的标题中指定什么吗?
My request:
我的请求:
URL --- http://localhost:3000/exercises.json
网址 --- http://localhost:3000/exercises.json
Header:
标题:
Content-Type - application/json
Raw data:
原始数据:
{
"exercise": {
"subbodypart_ids": [
"1",
"2"
],
"name": "Exercise14"
},
"image_file_name": "Pressurebar Above.jpg",
"image":"******base64 Format*******"
}
回答by TheWhiteRabbit
Your operation did not fail.
您的操作没有失败。
Your backend service is saying that the response type it is returning is not provided in the AcceptHTTP header in your Client request.
您的后端服务说它返回的响应类型未在您的客户端请求的AcceptHTTP 标头中提供。
Ref: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
参考:http: //en.wikipedia.org/wiki/List_of_HTTP_header_fields
- Find out the response (content type) returned by Service.
- Provide this (content type) in your request Accept header.
- 找出 Service 返回的响应(内容类型)。
- 在您的请求 Accept 标头中提供此(内容类型)。
回答by ashutosh raina
406 Not Acceptable The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.
406 Not Acceptable 请求标识的资源只能根据请求中发送的accept头生成具有不可接受的内容特征的响应实体。
406 happens when the server cannot respond with the accept-header specified in the request. In your case it seems application/json for the response may not be acceptable to the server.
当服务器无法使用请求中指定的接受标头进行响应时,会发生 406。在您的情况下,服务器似乎无法接受响应的 application/json。
回答by itamaryu
You mentioned you're using Ruby on Rails as a backend. You didn't post the code for the relevant method, but my guess is that it looks something like this:
您提到您使用 Ruby on Rails 作为后端。您没有发布相关方法的代码,但我的猜测是它看起来像这样:
def create
post = Post.create params[:post]
respond_to do |format|
format.json { render :json => post }
end
end
Change it to:
将其更改为:
def create
post = Post.create params[:post])
render :json => post
end
And it will solve your problem. It worked for me :)
它会解决你的问题。它对我有用:)
回答by etusm
You can also receive a 406 response when invalid cookies are stored or referenced in the browser - for example, when running a Rails server in Dev mode locally.
当在浏览器中存储或引用无效 cookie 时,您还可以收到 406 响应 - 例如,在本地以开发模式运行 Rails 服务器时。
If you happened to run two different projects on the same port, the browser might reference a cookie from a different localhost session.
如果您碰巧在同一个端口上运行两个不同的项目,浏览器可能会引用来自不同本地主机会话的 cookie。
This has happened to me...tripped me up for a minute. Looking in browser > Developer Mode > Network showed it.
这发生在我身上......让我绊了一分钟。查看浏览器>开发人员模式>网络显示它。
回答by rogerdpack
"Sometimes" this can mean that the server had an internal error, and wanted to respond with an error message (ex: 500 with JSON payload) but since the request headers didn't say it accepted JSON, it returns a 406 instead. Go figure. (in this case: spring boot webapp).
“有时”这可能意味着服务器有一个内部错误,并想用错误消息(例如:500 和 JSON 有效负载)进行响应,但由于请求标头没有说它接受 JSON,它返回 406。去搞清楚。(在这种情况下:spring boot webapp)。
In which case, your operation did fail. But the failure message was obscured by another.
在这种情况下,您的操作确实失败了。但是失败信息被另一个信息掩盖了。
回答by Kadir Erturk
In my case, I added:
就我而言,我补充说:
Content-Type: application/x-www-form-urlencoded
solved my problem completely.
完全解决了我的问题。
回答by JP Ventura
const request = require('request');
const headers = {
'Accept': '*/*',
'User-Agent': 'request',
};
const options = {
url: "https://example.com/users/6",
headers: headers
};
request.get(options, (error, response, body) => {
console.log(response.body);
});
回答by Alfredo Morales Pinzón
If you are using 'request.js' you might use the following:
如果您使用的是“request.js”,您可以使用以下内容:
var options = {
url: 'localhost',
method: 'GET',
headers:{
Accept: '*/*'
}
}
request(options, function (error, response, body) {
...
})
回答by Francois Borgies
In my case for a API in .NET-Core, the api is set to work with XML (by default is set to response with JSON), so I add this annotation in my Controller :
对于 .NET-Core 中的 API,该 API 设置为使用 XML(默认设置为使用 JSON 响应),因此我在我的 Controller 中添加了此注释:
[Produces("application/xml")]
public class MyController : ControllerBase {...}
Thank you for putting me on the path !
谢谢你让我走上这条路!

