在使用@RequestBody StreamSourcefind out时,如果请求体中的xml无效StreamSource则会抛出异常(导致400 Bad Request)并且我无法处理它(告诉客户端什么是坏的).
有没有办法处理这种异常?
HTTP 1.1声明:"客户端必须在所有HTTP/1.1请求消息中包含主机头字段"
但是,我正在处理的机器会发出这个确切的POST(包含坐标),我无权更改:
POST /touch HTTP/1.1
Content-type: application/x-www-form-urlencoded
Content-Length: <n>
x=<int x>&y=<int y>
Run Code Online (Sandbox Code Playgroud)
400 Bad Request由于缺少Host头字段,Tomcat 7立即响应,POST永远不会到达我的servlet.有什么方法可以避免这个错误响应并使用servlet处理POST以支持这些旧机器吗?
我正在研究MVC 4项目.
我有一个在Ajax Post请求完成时执行的Action.
在某些情况下,我可以确切地确定,我必须Status将Response对象的属性设置为HttpBadRequestvalue,并返回包含一些数据的JSON对象以显示给最终用户.
问题是我无法在javascript方法中收到JSON对象,我正在接收其他内容.这是因为我将Response的Status属性设置为HttpBadRequest值.
这是细节
行动
// this method will executed when some Ajax Post request.
[HttpPost]
public ActionResult Delete(int id)
{
// some code here ......
// in some case we will determine an error like this
if(error)
{
HttpContext.Response.Clear();
HttpContext.Response.TrySkipIisCustomErrors = true;
HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new
{
Message = string.Format(format, values),
Status = messageType.ToString()
});
}
}
Run Code Online (Sandbox Code Playgroud)
我想从这样的javascript函数中读取这个返回的JSON对象
使用Javascript
function OnDeleteFailed(data) {
debugger;
var try1 = $.parseJSON(data.responseText);
var try2 …Run Code Online (Sandbox Code Playgroud) 所以,将要http://localhost:3000/%ED扔ActionController::BadRequest.
但是如果返回404页面(就像大多数网站那样)我真正想要的是什么.
例如,Twitter会说:
一个或多个参数/%ED中的Unicode值无效
而我,我在开发中得到了它(生产中有500个错误页面):

我尝试了显而易见rescue_from但错误甚至不是我的应用程序,因为它在路由堆栈中引发.
任何的想法?我想过中间件,但不知道我会怎么做?
在我的 MVC 应用程序中,当一个 POST 请求被发送到服务器(通过 jQuery)并且发生验证错误时,会返回一个 400 Bad Request,正如预期的那样:
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
Date: Thu, 26 Feb 2015 08:35:50 GMT
Content-Length: 174
{"ReturnValue":null,"Results":[{"Message":"The xyz field is required.","ErrorNumber":123,"Severity":1}]}
Run Code Online (Sandbox Code Playgroud)
这在我的本地机器上按预期工作。但是,当我将应用程序部署到服务器时,完全相同的请求的响应看起来不同:
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Date: Thu, 26 Feb 2015 08:37:01 GMT
Content-Length: 24
Invalid Request
Run Code Online (Sandbox Code Playgroud)
请注意,内容类型为text/html,并且响应正文不再包含 JSON。
这可能是什么原因?我会很感激一个指针从哪里开始调试。
我正在尝试使用 oauth2 从 Google 请求访问和刷新令牌,我得到的只是“HTTP/1.1 400 错误请求”错误。
我在 Delphi XE5 中使用 IdHTTP 和 IdSSLIOHandlerSocketOpenSSL 处理程序。我有我的客户端 ID、客户端密钥和 API 密钥。我有授权码。
IdHTTP 配置为:
AllowCookies = True
HandleRedirects = True
HTTPOptions.hoKeepOrigProtocol = True
HTTPOptions.hoNoProtocolErrorException = True
Run Code Online (Sandbox Code Playgroud)
其他一切都是默认的。
有什么我应该事先做的事情吗,比如身份验证?
创建 Win32 应用程序时,redirect_uri 的相关性是什么?
将不胜感激地收到所有帮助。
这是我的代码:
var Params: TStringList;
Resp: TStringStream;
URI: String;
begin
Params := TStringList.Create;
Resp := TStringStream.Create;
try
URI := 'https://accounts.google.com/o/oauth2/token';
Params.Add('client_id=' + clientID);
Params.Add('client_secret=' + clientSecret);
Params.Add('code=' + authCode);
Params.Add('redirect_uri=http://localhost');
Params.Add('grant_type=authorization_code');
IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
IdHTTP.Post(URI, Params, Resp);
Memo1.Lines.LoadFromStream(Resp);
Memo1.Lines.Insert(0, IdHTTP.ResponseText);
finally
FreeAndNil(Params); …Run Code Online (Sandbox Code Playgroud) 我想发布一个更新字段“名称”的突变,它是 JSONString。当我这样做时,我得到了一个响应 - 400 BAD REQUEST,但是当我尝试其他突变(使用 String 字段类型)时,它变得顺利,结果正是我想要的。
function updateUserData() {
var xhr = new XMLHttpRequest(),
token = "BTNsngsfgfstnrw64wNsrgnws"
var mutation = `mutation {
addPosition( input: {
name: "{\"pl\": \"Devlo\"}"
}) {
result {
name
}
}
}`;
$.ajax({
beforeSend: (xhr) => xhr.setRequestHeader('Authorization', 'Basic ' + token),
type: 'POST',
url: 'http://46.17.113.45/graphql',
data: JSON.stringify({ 'query': 'mutation { addPosition( input: { name: "{\\"p\\": \\"Develo\\"}" }) { result { name
contentType: 'application/json'
}).done(function(response) {
console.log(response)
});}
Run Code Online (Sandbox Code Playgroud)
在 GraphiQL 中,这种变异效果很好。返回值是查询中的确切内容。JSONString 和那些引号是否有问题,或者问题出在其他地方?令牌、类型、内容类型都很好 - 其他带有查询或突变的 POST …
MVC 核心 2.0 Web 应用程序在浏览器中启动应用程序时遇到问题,它抛出错误请求 - 请求太长和 HTTP 错误 400,请求标头的大小太长。即使我清除了所有 cookie。第一次它将再次加载到错误的请求页面。
有没有办法可以将消息添加到BadRequest操作结果中,并使该消息对外部客户端(例如 Postman)可见?我正在使用 ASP.NET Core 3.1。
我的部分代码包含在下面。我想说的是问题是什么\xe2\x80\x94e.g.,id正文中发送的内容与从 URL 中获取的内容不同。现在,我正在使用Error我制作的一个对象,其中包含错误代码和消息。但当我发送请求时,这些在邮递员中不可见。
public ActionResult PutColour(int id, Colour colour)\n{\n if (id != colour.Id)\n {\n return BadRequest(new Error("IDNotTheSame","ID from URL is not the same as in the body."));\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n httprequest bad-request asp.net-core asp.net-core-webapi asp.net-core-3.1
我在 Twilio API 中收到“400 - 错误请求”错误,但找不到原因。
这是我的要求:
curl --location --request POST 'https://verify.twilio.com/v2/Services/xxxxxxxxxxxxxx/Verifications' \
--header 'Authorization: Basic xxxxxxxxx' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'Channel=sms' \
--data-urlencode 'To=+44xxxxxxxxxx'
Run Code Online (Sandbox Code Playgroud)
我收到回复:
curl --location --request POST 'https://verify.twilio.com/v2/Services/xxxxxxxxxxxxxx/Verifications' \
--header 'Authorization: Basic xxxxxxxxx' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'Channel=sms' \
--data-urlencode 'To=+44xxxxxxxxxx'
Run Code Online (Sandbox Code Playgroud)
响应包含RequestID标头,但在 Twilio 门户中无法通过 查找错误RequestID。
bad-request ×10
asp.net-core ×2
http ×2
json ×2
access-token ×1
asp.net ×1
c# ×1
cookies ×1
delphi ×1
google-oauth ×1
graphql ×1
hostheader ×1
http-1.1 ×1
httprequest ×1
iis ×1
indy ×1
javascript ×1
spring ×1
spring-mvc ×1
tomcat ×1
twilio-api ×1