我有以下使用郵遞員的 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 Bad Request 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":"錯誤處理給定來自請求的輸入"}}
uj5u.com熱心網友回復:
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321154.html
