json - 如何以角度发送卷曲请求
问题描述
我有以下使用邮递员的 curl 请求,我想创建一个 Angular 的 http 请求,它做同样的事情
curl --location --request POST 'http://api.deepai.org/api/fast-style-transfer' \
--header 'api-key: myKey' \
--form 'content="https://www.dmarge.com/cdn-cgi/image/width=1200,quality=85,fit=scale-down,format=auto/https://www.dmarge.com/wp-content/uploads/2021/01/dwayne-the-rock-.jpg"' \
--form 'style="https://images.fineartamerica.com/images/artworkimages/mediumlarge/3/starry-night-print-by-vincent-van-gogh-vincent-van-gogh.jpg"'
这是我到目前为止所拥有的,但我遇到了错误
constructor(private http: HttpClient) {}
ngOnInit() {}
async style(){
const url = 'http://api.deepai.org/api/fast-style-transfer';
const headers = new HttpHeaders()
.set('api-key', 'myKey');
const resp = await this.http.post(url, { content: 'https://www.dmarge.com/cdn-cgi/image/width=1200,quality=85,fit=scale-down,format=auto/https://www.dmarge.com/wp-content/uploads/2021/01/dwayne-the-rock-.jpg',
style: 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/3/starry-night-print-by-vincent-van-gogh-vincent-van-gogh.jpg'}, {
headers
}).toPromise().then();
console.log(resp);
}
错误:
XHRPOSThttp://api.deepai.org/api/fast-style-transfer [HTTP/1.1 400 错误请求 1993ms]
GEThttp://localhost:8100/undefined [HTTP/1.1 404 Not Found 28ms]
错误错误:未捕获(承诺):HttpErrorResponse:{“headers”:{“normalizedNames”:{},“lazyUpdate”:null},“status”:400,“statusText”:“Bad Request”,“url”: "http://api.deepai.org/api/fast-style-transfer","ok":false,"name":"HttpErrorResponse","message":"http://api.deepai 的Http失败响应.org/api/fast-style-transfer : 400 Bad Request","error":{"err":"error processing given inputs from request"}}
解决方案
POST 请求正文应为 JSON 格式,因此请尝试构建一个FormData
对象,如以下代码片段所示。
此外,将标头设置为接受 JSON 格式的数据。
async style() {
const url = 'http://api.deepai.org/api/fast-style-transfer';
const headers = new HttpHeaders()
.set('accept', 'application/json')
.set('api-key', 'myKey');
let requestBody = new FormData();
requestBody.append('content', 'https://www.dmarge.com/cdn-cgi/image/width=1200,quality=85,fit=scale-down,format=auto/https://www.dmarge.com/wp-content/uploads/2021/01/dwayne-the-rock-.jpg');
requestBody.append('style', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/3/starry-night-print-by-vincent-van-gogh-vincent-van-gogh.jpg');
const resp = await this.http.post(url, requestBody, {
headers: headers
}).toPromise().then();
console.log(resp);
}
推荐阅读
- android - Android - alarmManager.setRepeating 不重复
- wordpress - Cloudflare 优化技术(免费计划)
- python - Beautifulsoup Python:Goibibo 酒店价格
- asp.net-core - 无法向 .NET Core MVC 发出 HTTP 发布请求
- javascript - 不能在三元方程中访问对象的属性,但在 JSX 中可以正常工作?
- python - 在 python 数据框中查找最小值,仅选择其他 df 或列表中包含的行或列
- python - 我无法从 arabert.preprocess_arabert 导入预处理
- github-actions - 推送事件不会触发推送路径上的工作流(github 操作)
- javascript - 无法连接到:未定义 - 在 Electron 应用程序中连接到 Windows 身份验证上的 MSSQL Server 时的自签名证书
- c# - Linq 查询从表中获取最近插入的 Id