(请参阅2015年8月20日下方的编辑部分)
最近我遇到了类似的问题(Get请求仅适用于斜杠(spring REST注释)),解决方案是在@RequestMapping的值中添加一个正则表达式(另请参见Spring MVC @PathVariable被截断)。
但是,现在我们已经意识到问题仍然存在,但仅适用于以.com或.org结尾的电子邮件地址。这很奇怪。
不久,我使用Spring Annotations构建REST服务。我有一个带有三个参数的GET请求,最后一个是电子邮件。
我的控制器:
@RequestMapping(method = GET, value = "/{value1}/{value2}/{value3:.+}",
produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
public ResponseEntity<MyResponseType> getMyResource(
@ApiParam(value = "...",...)
@PathVariable("value1") String value1,
@ApiParam(value = "...",...)
@PathVariable("value2") String value2,
@ApiParam(value = "...",...)
@PathVariable("value3") String value3) {
//...
}
Run Code Online (Sandbox Code Playgroud)
如果我致电: http:// myserver:8080 / myresource / value1 / value2 / value3
如果value3 = email@domain.de / co.uk / .name / .biz /.info,则完全没有问题。
但是,对于某些顶级域名(到目前为止,.com,.org),我收到Http状态码406(不接受)。
如果添加斜杠,它将起作用:
我在我的站点中创建了一个 API,我试图从 python 调用一个 API,但我总是得到 406 作为响应,但是,如果我将 url 与参数一起放在浏览器中,我可以看到正确的答案
我已经在您可以测试自己的 API 的页面中进行了一些测试,我在浏览器中对其进行了测试并且工作正常。我已经跟进了一本手册,解释了如何从 python 调用 API,但我没有得到正确的响应:(
这是带有参数的 API 的 URL:https ://icassy.com/api/login.php ? usuario_email = warles34%40gmail.com & usuario_clave =123
这是我用来从 Python 调用 API 的代码
import requests
urlLogin = "https://icassy.com/api/login.php"
params = {'usuario_email': 'warles34@gmail.com', 'usuario_clave': '123'}
r = requests.get(url=urlLogin, data=params)
print(r)
print(r.content)
Run Code Online (Sandbox Code Playgroud)
我得到:
<Response [406]>
b'<head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>'
Run Code Online (Sandbox Code Playgroud)
我应该以 JSON 格式接收成功消息和 apikey,如下所示:
{ …
哪种HTTP状态代码最适合用于“客户端请求的格式错误”,例如,客户端请求format=json的数据只能以XML格式提供?我倾向于使用406,但这是指Acceptheders,而格式是在我的用例中作为查询字符串中的参数给出的。
406这里的代码正确吗?
谢谢
我试图在jackson-mapper-asl库的帮助下将对象列表转换为json ,但作为响应我得到了http 406错误.jackson-mapper-asl图书馆在我的班级路径上.以下是我的Spring MVC控制器代码:
@RequestMapping(value="/find-sub-categories/{parentId}", method=RequestMethod.GET)
@ResponseBody public List<ProductCategory> findSubCategories(@PathVariable(value="parentId") ProductCategory productCategory) {
logger.info("In ADMIN findSubCategories Contoller AJAX GET");
List<ProductCategory> categories = productCategoryService.findAllChildProductCategoriesByParent(productCategory);
return categories;
}
Run Code Online (Sandbox Code Playgroud)
我的Ajax请求代码:
$.ajax({
url: url+"/"+parentId,
type: 'GET',
success: function(result) {
var arr = $.parseJSON(result);
-----------------------
Run Code Online (Sandbox Code Playgroud) 我对用于表单的Post查询有问题.每次我尝试验证表单时都会出现"406不可接受"错误,而Object.data为空白.
var edit = function(form){
var token = window.localStorage.getItem('token');
$ionicLoading.show();
return $http({
method : 'POST',
url : API.url + '/user',
headers : {Authorization : 'Bearer ' + token},
transformRequest: function(data, headers){
console.log(headers);
headers = angular.extend({}, headers, {'Content-Type': 'application/json;charset=UTF-8'});
console.log(headers);
console.log(data);
console.log(angular.toJson(data));
return angular.toJson(data); // this will go in the body request
},
data : form
}).then(function(result) {
console.dir(result.data);
},function errorCallback(response) {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
};
我不明白为什么不接受..
尝试使用RestSharp将请求发布到第三方应用程序时,我收到406错误.我是REST的新手,所以我不得不承认我甚至不知道你可以添加标题.我尝试添加这些,但我仍然遇到同样的问题:
var client = new RestClient(myURL);
RestRequest request = new RestRequest("restAction", Method.POST);
request.AddHeader("Accept", "text/plain");
request.AddHeader("Content-Type", "text/plain");
request.AddParameter("parameter1", param1);
request.AddParameter("parameter2", param2);
var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
根据我的阅读,这可能是处理名为"接受"的标题.是对的吗?
知道会发生什么吗?