HTTP响应是根据HTTP规范由主体和头部形式的元数据组成的网络消息。也可以在软件框架和库中引用HttpResponse类来自动执行相关功能
我正在 Java Spring Boot 中测试我的 API 服务器端代码。服务器端已经响应了,但是客户端一直在等待响应体,直到客户端超时,服务器端...
我突然想到,为了测试处理不良响应的客户端代码,拥有一个始终返回 500 的 URL 会很有用。我可以想到其他项目......
我不使用blade.php,因为该文件是由管理员上传并进入存储的。并且在html文件中有一个可以填写的表格,它总是得到419。 我尝试过使用@csrf b...
我正在将 Flask-restx 用于我的 REST-API。在那里我注意到一个奇怪的行为,大整数值似乎返回错误。 我正在使用这个最小的代码片段来重现该问题: @ns.r...
在获取 HTTP 400 响应代码的 WCF 客户端上获取响应
我正在使用下一个代码(在 .NET 7 中)作为我无法控制的 Java SOAP Web 服务的客户端: 命名空间 ActuacionMov { [System.CodeDom.Compiler.GenerateCodeAttribute("微软。
C# 从 HttpResposeMessage 获取字节数组
我有一个 API,它以字节数组的形式返回 PDF 文件: HttpResponseMessage 文件响应 = 新的 HttpResponseMessage { Content = new ByteArrayContent(report) //这里字节数组>80000 in ...
将 httpResponse 转换为 ResponseEntity 为 null C#
我有这个代码: 私有类 TResponse { 公共布尔IsSuccessful { 得到;放; } 公共字符串错误代码{获取;放; } 公共字符串ErrorMessage { 获取;放; } } var 响应 = 客户端。
在restful API中,当响应自定义错误消息时,正确的响应代码是什么?例如“用户名不存在” 一位同事认为应该是代码 400,但不...
在 Shopify 应用程序的 Django HttpResponse 对象中设置 Content-Type
我正在使用 Django 开发 Shopify 应用程序,我将其托管在带有 nginx 和 Gunicorn 的 VPS 上。 我正在尝试将 HttpResponse 对象的 Content-Type 更改为 application/liquid,这样我就可以...
ASP.NET MVC 服务器登录无效后,如何捕获返回的 Json 响应中的错误文本并显示在 Angular 客户端中?
Angular 9 客户端登录页面 我想在 元素中显示特定返回的错误消息,而不是当前显示的通用消息。 Angular 9 客户端登录页面 我想在 <h4> 元素中显示具体返回的错误消息,而不是当前显示的通用消息。 <div class="navbar bg-info mb-1"> <a class="navbar-brand text-white">The ACTS Factory Authentication</a> </div> <!-- I want to put the specific error condition here --> <h4 *ngIf="showError" class="p-2 bg-danger text-white"> Invalid username or password! </h4> <form novalidate #authForm="ngForm" class="m-3" title="LoginForm"> <div class="form-group"> <label>Name:</label> <input #name="ngModel" name="name" class="form-control" [(ngModel)]="authService.name" required /> <div *ngIf="name.invalid" class="text-danger"> Please enter your user name </div> </div> <div class="form-group"> <label>Password:</label> <input type="password" #password="ngModel" name="password" class="form-control" [(ngModel)]="authService.password" required /> <div *ngIf="password.invalid" class="text-danger"> Please enter your password </div> </div> <div class="text-center pt-2"> <button type="button" class="btn btn-primary font-weight-bold" [disabled]="authForm.invalid" (click)="login()"> Please Login </button> </div> </form> Angular 9 组件 我正在从这个 Angular 客户端组件登录。当响应状态代码为 400 Bad Request 时,result 为 false,一般错误消息显示在 <h4> 元素中。这里的login()方法订阅了认证服务存储库中的Login()方法。返回的响应对象或包含错误文本的属性必须返回此处才能添加到 html 显示中。 import { Component } from "@angular/core"; import { AuthenticationService } from "./authentication.service"; @Component({ templateUrl: "authentication.component.html" }) export class AuthenticationComponent { showError: boolean = false; constructor(public authService: AuthenticationService) { } login() { this.showError = false; this.authService.login().subscribe(result => { this.showError = !result }); } } Angular 9 登录方法在客户端认证服务存储库中 此登录方法调用数据源的可观察 login() 方法,确定是否已收到有效(真或假)响应,并将响应中包含的返回属性映射到局部变量。 login(): Observable<boolean> { this.authenticated = false; return this.dataSource.login(this.userName, this.userPwd).pipe( map(response => { if (response) { this.callbackUrl = null; this.authenticated = true; this.password = null this.router.navigateByUrl(this.callbackUrl || "appmenu"); } this.profileEmail = null; this.profilePhone = null; if (this.dataSource.teamUser) { this.isTeamUser = this.dataSource.teamUser; } else { this.isTeamUser = false; } return this.authenticated = true; }), catchError(e => { this.authenticated = false; [I have tried to capture the error text here] return of(false); })); } Angular 9 客户端身份验证数据源登录方法 这是客户端的数据源;它向服务器发送http请求并接收http响应。它还将响应中的属性映射到局部变量,确定目标 URL,并跟踪必要的标头。 auth_Header: HttpHeaders = new HttpHeaders(); auth_token: string = null; login(theName: string, thePassword: string): Observable<boolean> { return this.http.post<any>("/api/account/login", { name: theName, password: thePassword }) .pipe(map(response => { [I have tried capturing the error here and exiting with response.success = false] this.auth_token = response.success ? response.token : null; // JWT token generated in server-side AccountController this.auth_Header = this.auth_Header.set("Authorization", "Bearer<" + this.auth_token + ">"); this.authCookie.JWTauthcookie = this.auth_token; // save for the other datasources this.loginTime = response.loginTime; // to return user's registered email and phonenumber to client this.profileEmail = ""; this.profileEmail = response.profileEmail; this.profilePhone = ""; this.profilePhone = response.profilePhone; } this.isAuthenticated = true; return response.success; })); ASP.NET Core MVC 帐户控制器 这是处理登录请求的 ASP.NET Core MVC 控制器,在 ASP.NET Core Identity 中搜索用户名和密码(代码中未显示),并在登录成功时返回 200 状态代码和 JSON 对象。我现在返回两个特定的登录错误条件(未找到用户或密码无效),我想在无效登录尝试后在客户端上显示它们。我尝试使用 OK 而不是 BadRequest() 状态代码 400 返回错误。但是,这对客户端没有帮助。 任何和所有的想法将不胜感激。谢谢。 [HttpPost("/api/account/login")] public async Task<IActionResult> Login([FromBody] LoginViewModel creds) { LoginError = ""; bool loginResult = false; object myReturnObject = new object(); if (ModelState.IsValid) { // go look for existing Identity user and try to sign in loginResult = await DoLogin(creds); if (loginResult) { myReturnObject = "{ " + '\n' + " " + '"' + "success" + '"' + ":" + '"' + signInResult.Succeeded.ToString() + '"' + "," + '\n' + " " + '"' + "token" + '"' + ':' + '"' + bearerJWTokenString + '"' + "," + '\n' + " " + '"' + "loginTime" + '"' + ':' + '"' + DateTime.Now.ToShortTimeString() + '"' + "," + '\n' + " " + '"' + "profileEmail" + '"' + ':' + '"' + profileEmail + '"' + "," + '\n' + " " + '"' + "profilePhone" + '"' + ':' + '"' + profilePhone + '"' + "," + '\n' + " " + '"' + "team" + '"' + ':' + '"' + loggedInUserType.ToString() + '"' + "," + '\n' + "}"; // good login returns this object as a Json file in // the HTTP Response body return Ok(myReturnObject); } else { // if bad username or pwd - this returns http status // code 400 "Bad Request" plus the LoginError json object return BadRequest("{" + '\n' + " " + '"' + "InvalidLogin" + '"' + ':' + '"' + "True" + '"' + '\n' + " " + '"' + "LoginError" + '"' + ':' + '"' + LoginError + '"' + '\n' + "}"); } } // bad model state returns - http error 400 Bad Request return BadRequest(ModelState.ValidationState.ToString()); } 客户端返回的JSON对象截图: .subscribe( data => {/* success action*/}, (error: HttpErrorResponse) => {/* error action */ console.error(error)}, () => {/* complite (after success OR error) action */} )
MVC Server 登录无效后,如何捕获返回的 Json 响应中的错误文本并将其显示给 Angular 客户端上的用户?
Angular 9 客户端登录页面——我想在 h4 元素中显示特定返回的错误消息,而不是当前显示的通用消息。 Angular 9 客户端登录页面 - 我想在 h4 元素中显示特定返回的错误消息,而不是当前显示的通用消息。 <div class="navbar bg-info mb-1"> <a class="navbar-brand text-white">The ACTS Factory Authentication</a> </div> <h4 *ngIf="showError" class="p-2 bg-danger text-white"> Invalid username or password! **I want to put the specific error condition here** </h4> <form novalidate #authForm="ngForm" class="m-3" title="LoginForm"> <div class="form-group"> <label>Name:</label> <input #name="ngModel" name="name" class="form-control" [(ngModel)]="authService.name" required /> <div *ngIf="name.invalid" class="text-danger"> Please enter your user name </div> </div> <div class="form-group"> <label>Password:</label> <input type="password" #password="ngModel" name="password" class="form-control" [(ngModel)]="authService.password" required /> <div *ngIf="password.invalid" class="text-danger"> Please enter your password </div> </div> <div class="text-center pt-2"> <button type="button" class="btn btn-primary font-weight-bold" [disabled]="authForm.invalid" (click)="login()"> Please Login </button> </div> </form> Angular 9 组件——我从这个 Angular 客户端组件登录。 当响应状态代码为 400 Bad Request 时,“result”为 false,一般错误消息显示在 h4 元素中。 这里的login()方法订阅了身份验证服务存储库中的Login()方法。 返回的 Response 对象或包含错误文本的属性必须返回此处才能添加到 html 显示中。 import { Component } from "@angular/core"; import { AuthenticationService } from "./authentication.service"; @Component({ templateUrl: "authentication.component.html" }) export class AuthenticationComponent { showError: boolean = false; constructor(public authService: AuthenticationService) { } login() { this.showError = false; this.authService.login().subscribe(result => { this.showError = !result }); } } 客户端身份验证服务存储库中的 Angular 9 登录方法——此登录方法调用数据源的 Observable login() 方法,确定是否已收到有效(真或假)响应,并将响应中包含的返回属性映射到局部变量. login(): Observable<boolean> { this.authenticated = false; return this.dataSource.login(this.userName, this.userPwd).pipe( map(response => { if (response) { this.callbackUrl = null; this.authenticated = true; this.password = null this.router.navigateByUrl(this.callbackUrl || "appmenu"); } this.profileEmail = null; this.profilePhone = null; if (this.dataSource.teamUser) { this.isTeamUser = this.dataSource.teamUser; } else { this.isTeamUser = false; } return this.authenticated = true; }), catchError(e => { this.authenticated = false; [I have tried to capture the error text here] return of(false); })); } Angular 9 客户端身份验证数据源登录方法 -- 这是客户端的数据源;它向服务器发送http请求并接收http响应。 它还将响应中的属性映射到局部变量,确定目标 URL,并跟踪必要的标头。 auth_Header: HttpHeaders = new HttpHeaders(); auth_token: string = null; login(theName: string, thePassword: string): Observable<boolean> { return this.http.post<any>("/api/account/login", { name: theName, password: thePassword }) .pipe(map(response => { [I have tried capturing the error here and exiting with response.success = false] this.auth_token = response.success ? response.token : null; // JWT token generated in server-side AccountController this.auth_Header = this.auth_Header.set("Authorization", "Bearer<" + this.auth_token + ">"); this.authCookie.JWTauthcookie = this.auth_token; // save for the other datasources this.loginTime = response.loginTime; // to return user's registered email and phonenumber to client this.profileEmail = ""; this.profileEmail = response.profileEmail; this.profilePhone = ""; this.profilePhone = response.profilePhone; } this.isAuthenticated = true; return response.success; })); MVC 帐户控制器——这是处理登录请求的 MVC 控制器,在 AspNetCore Identity 中搜索用户名和密码(代码中未显示),并在登录成功时返回 200 状态代码和 Json 对象。 我现在返回两个特定的登录错误条件(未找到用户或密码无效),我想在无效登录尝试后在客户端上显示它们。 我尝试使用“OK”而不是 BadRequest() 状态代码 400 返回错误。但是,这对客户端没有帮助。 任何和所有的想法将不胜感激。 谢谢。 [HttpPost("/api/account/login")] public async Task<IActionResult> Login([FromBody] LoginViewModel creds) { LoginError = ""; bool loginResult = false; object myReturnObject = new object(); if (ModelState.IsValid) { loginResult = await DoLogin(creds); // go look for existing Identity user and try to sign in if (loginResult) { myReturnObject = "{ " + '\n' + " " + '"' + "success" + '"' + ":" + '"' + signInResult.Succeeded.ToString() + '"' + "," + '\n' + " " + '"' + "token" + '"' + ':' + '"' + bearerJWTokenString + '"' + "," + '\n' + " " + '"' + "loginTime" + '"' + ':' + '"' + DateTime.Now.ToShortTimeString() + '"' + "," + '\n' + " " + '"' + "profileEmail" + '"' + ':' + '"' + profileEmail + '"' + "," + '\n' + " " + '"' + "profilePhone" + '"' + ':' + '"' + profilePhone + '"' + "," + '\n' + " " + '"' + "team" + '"' + ':' + '"' + loggedInUserType.ToString() + '"' + "," + '\n' + "}"; return Ok(myReturnObject); // good login returns this object as a Json file in the HTTP Response body } else { // if bad username or pwd - this returns http status code 400 "Bad Request" plus the LoginError json object return BadRequest("{" + '\n' + " " + '"' + "InvalidLogin" + '"' + ':' + '"' + "True" + '"' + '\n' + " " + '"' + "LoginError" + '"' + ':' + '"' + LoginError + '"' + '\n' + "}"); } } return BadRequest(ModelState.ValidationState.ToString()); // bad model state returns - http error 400 Bad Request } 客户端返回的Json对象图片 [![Login error text message][1]][1] .subscribe( data => {/* success action*/}, (error: HttpErrorResponse) => {/* error action */ console.error(error)}, () => {/* complite (after success OR error) action */} )
AttributeError:“HTTPResponse”对象没有属性“replace”
嗨,我收到上述错误。为什么它会弹出,我错过了什么以及如何解决它?谢谢 尝试: 将 urllib.request 导入为 urllib2 除了导入错误: 导入 urllib2 来自 html2text
我正在设计一个用于注册课程注册的 REST API。在我的端点中,我可以发布注册: 发布到 http://my-api/class/learn-rest/enrollment 这将创建一个新的注册。
如何在FastAPI的RedirectResponse中添加body内容?
我想在 /check_base 端点内使用音频文件。但是,我无法在 FastAPI 的 RedirectResponse 正文中发送文件。 /check 端点返回一个 RedirectResponse,仅
JSON 回复的多种 http“内容类型”是什么? 除了“application/json”内容类型之外,我们还可以期待任何其他内容类型吗? 同样的,我们还有其他内容吗
如何从 Power Automate 工作流 HTTP 响应检索特定错误详细信息
我当前正在使用名为“收到 Webhook 请求时发布到频道”的 Power Automate 工作流程。我遇到过一个问题,大消息会导致“413 Entity Too L...
如何在 .NET Core 3.1.x 上写入 HttpContext.Response.Body
我一直在尝试编写或设置 HttpContext.Response.Body 但无济于事。我发现的大多数示例都使用 Body.WriteAsync,但参数与示例不同。我试过了
在 Typescript 中使用 Jest 模拟获取返回响应
我有一个调用 fetch() 并处理其响应数据的不同情况的函数。 当尝试使用 Jest 模拟全局获取函数时,我在响应类型方面遇到了麻烦。 我只是...
在 Firefox DevTools Network 选项卡中,如何仅查看失败的请求?
我希望能够查看所有有错误响应(任何类型)的请求。我的意思是,响应状态代码为“4xx”或“5xx”的请求。 我发现...
在 Firefox DevTools Network 选项卡中,如何仅查看错误请求?
我希望能够看到所有错误的请求(任何类型)。我的意思是带有“4xx”或“5xx”等状态代码的请求。 我发现我可以输入 status-code:20...