use*_*396 14 angular angular4-httpclient
我们有一个.net WebAPI,它在post请求的主体中查找文件路径字符串并返回相应的图像.我正在努力使用新的httpClient成功地从Angular 4.3传递一个字符串.可能吗?其他东西正在使用端点,所以我真的不想创建一个字符串的'模型',所以我基本上可以复制它,但如果可能的话,将它传递给json.
WebAPI方法签名:
public HttpResponseMessage ImagesFromPaths(HttpRequestMessage request, [FromBody] string path)
Run Code Online (Sandbox Code Playgroud)
现行服务方式:
getImage(path: string): Observable<any> {
return this.http.post(
`${apiUrl}`,
{ path },
{ headers: new HttpHeaders({
'Content-Type': 'application/json',
}), responseType: 'blob',
})
}
Run Code Online (Sandbox Code Playgroud)
要成为一件容易的事吗?
mat*_*219 20
几乎所有代码都很好.主要问题是Content-Type标题.如果要将.NET带有[FromBody]注释的字符串发送到REST API 并使用application/json标头值,则应添加""到路径参数中,例如"test_value":
return this.http.post(
`${apiUrl}`,
`\"${path}\"` ,
{ headers: new HttpHeaders({
'Content-Type': 'application/json',
}), responseType: 'blob',
})
Run Code Online (Sandbox Code Playgroud)
您还可以使用x-www-form-urlencoded标头值.然后你必须通过你的参数以这种方式请求身体:
return this.http.post(
`${apiUrl}`,
`=${path}` ,
{ headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
}), responseType: 'blob',
})
Run Code Online (Sandbox Code Playgroud)
你可以得到path从query通过移除[fromBody]属性
public HttpResponseMessage ImagesFromPaths(HttpRequestMessage request, string path)
Run Code Online (Sandbox Code Playgroud)
并在post请求中的发送路径中${apiUrl}/${path}:
return this.http.post(
`${apiUrl}/${path}`,
{ headers: new HttpHeaders({
'Content-Type': 'application/json',
}), responseType: 'blob',
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4849 次 |
| 最近记录: |