[英]How do I send this json request body from angular using http client post method?
这是 Postman 示例,说明请求正文如何像我将 query_string 发送到 flask 后端并返回推文列表。
现在我想使用 http 客户端发布方法从 angular 应用程序发送发布请求
searchTerm 是 query_string
import { GoogleResponse } from './components/GoogleResponse.model';
import { environment } from './../environments/environment';
import { Injectable, Query } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { solr_Response } from './components/solr_response.model';
@Injectable({
providedIn: 'root'
})
export class SearchService {
private url: string = '3.22.175.82:5000/queryresult';
/* solr url */
constructor(private httpClient:HttpClient) { }
/**
*
* @param searchTerm
* @returns
*/
getSearchData(searchTerm: string): Observable<any> {
return this.httpClient.post(this.url, searchTerm);
}
}
我只是发送一个字符串作为请求正文,但它给了我错误我的猜测是正文需要在 object 中或者简单的字符串就足够了。 但我收到了这个错误
正如@vicatcu 所提到的, url
应该由有效的 URL 开头,以 http:// 或 Z5E0656C50B50A18 开头。 此外,您的端点需要一个 object { "query_string": "..." }
。 因此它应该是这样的:
private url = 'http://3.22.175.82:5000/queryresult';
...
getSearchData(searchTerm: string): Observable<any> {
return this.httpClient.post(this.url, { query_string: searchTerm });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.