我有一个API,其中包含服务器引发错误时出错的有用描述(状态= 500).该描述作为响应文本的一部分.我的客户端代码,使用Aurelia,通过aurelia-fetch-client
使用通用方法调用api 来进行调用:
function callRemoteService(apiName, timeout) {
return Promise.race([
this.http.fetch(apiName),
this.waitForServer(timeout || 5000) // throws after x ms
])
.then(response => response.json() )
.catch(err => {
if (err instanceof Response) {
// HERE'S THE PROBLEM.....
err.text().then(text => {
console.log('Error text from callRemoteService() error handler: ' + text);
throw new Error(text)
});
} else if (err instanceof Error) {
throw new Error(err.message);
} else {
throw new Error('Unknown error encountered from callRemoteService()');
}
});
}
Run Code Online (Sandbox Code Playgroud)
请注意,我想以一致的方式捕获服务器(获取或超时)错误,然后throw
只返回一个简单的错误消息到调用视图.我可以callRemoteService …
我们有一个服务于Aurelia静态文件和API的Web服务器,服务器受NTLM保护(在OWIN上使用集成Windows身份验证).
使用Aurelia Fetch Client时,我们可以成功点击API而不会出现问题.这是我们使用的配置:
constructor(private http: HttpClient){
http.configure(config => {
config
.withBaseUrl('api/')
.useStandardConfiguration();
});
Run Code Online (Sandbox Code Playgroud)
但是,当我们使用Aurelia Fetch Client时,我们得到了401 (Unauthorized)
(似乎缺少授权标头)
constructor(private client: HttpClient) {
client.configure(cfg => {
cfg
.withBaseUrl('http://localhost:80/api/someEndpoint')
.withDefaults({
headers: {
'Accept' : 'application/json',
'X-Requested-With': 'Fetch'
}
})
Run Code Online (Sandbox Code Playgroud)
关于如何解决这个问题的任何想法都非常感谢.
aurelia aurelia-fetch-client aurelia-http-client aurelia-framework
问题很简单:如何x-www-form-urlencoded
使用Aurelia Fetch客户端发布内容?
我需要将帖子发布到使用OWIN和Katana进行身份验证的简单ASP.NET Web API服务器.
我已经尝试过的一个例子:
var loginDTO = new FormData();
loginDTO.append('grant_type', 'password');
loginDTO.append('email', 'test');
loginDTO.append('password', 'test');
return this.http
.fetch(config.router.token, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: loginDTO
});
Run Code Online (Sandbox Code Playgroud)
显然,这没有按预期工作.如何正确发布示例中显示的数据?
如果是401响应,我如何拦截服务器端点响应并将aurelia应用程序重定向到登录页面?
我尝试了aurelia-fetch-client配置的"withInterceptor(responseError(){...})"方法,但我无法返回"new Redirect(loginPage)"...
任何人都知道如何做到这一点?
我试图从dotnet核心web api操作下载一个zip文件,但我无法使其工作.我尝试通过POSTMAN和我的Aurelia Http Fetch Client调用该操作.
我能够像我想要的那样创建ZipFile并将其存储在系统上,但无法修复它,因此它通过api返回zip文件.
用例:用户选择几个图片集并单击下载按钮.图片集的ID被发送到api并创建一个zipfile,其中包含用于保存图片的每个图片集的目录.该zipfile返回给用户,以便他/她可以将其存储在他们的系统上.
任何帮助,将不胜感激.
我的控制器动作
/// <summary>
/// Downloads a collection of picture collections and their pictures
/// </summary>
/// <param name="ids">The ids of the collections to download</param>
/// <returns></returns>
[HttpPost("download")]
[ProducesResponseType(typeof(void), (int) HttpStatusCode.OK)]
public async Task<IActionResult> Download([FromBody] IEnumerable<int> ids)
{
// Create new zipfile
var zipFile = $"{_ApiSettings.Pictures.AbsolutePath}/collections_download_{Guid.NewGuid().ToString("N").Substring(0,5)}.zip";
using (var repo = new PictureCollectionsRepository())
using (var picturesRepo = new PicturesRepository())
using (var archive = ZipFile.Open(zipFile, ZipArchiveMode.Create))
{
foreach (var id in ids)
{
// Fetch …
Run Code Online (Sandbox Code Playgroud) download zipfile asp.net-web-api .net-core aurelia-fetch-client
我有一个 dotnet 核心 api,它返回一个 FileContentResult ..
return new FileContentResult(bytes, contentType)
{
FileDownloadName = Path.GetFileName(request.Filename)
};
Run Code Online (Sandbox Code Playgroud)
通过邮递员,我可以完美地读出图像。现在我想通过 aurelia fetch 客户端读取图像,并将其显示在我的 html 视图中。这是我从api检索图像的功能。
public image(filename: string) {
return this.http.fetch(AppConfiguration.base_url + 'assets/image',
{
method: 'post',
body: json({
filename: filename
})
});
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用此值转换器转换响应中的 blob。但我不能让它工作
转换器:
export class BlobToUrlValueConverter {
public toView(blob) {
return URL.createObjectURL(blob);
}
}
Run Code Online (Sandbox Code Playgroud)
视图模型:
export class Dashboard {
public blob: any;
constructor(
public assets_service: AssetsService
) { }
async attached() {
let response = await this.assets_service.image('test.png');
this.blob = response.blob();
} …
Run Code Online (Sandbox Code Playgroud)