尝试使用字符串作为有效负载执行http.post请求

时间:2018-02-12 12:56:31

标签: angular rest http post

我尝试使用以下代码从我的角度应用程序向其余服务器发送http.post请求:

我正在使用:import { Http } from '@angular/http';

let headers = new Headers();
  headers.append('Content-Type', 'application/json');
let data: string = 'wing,wheel';
this.http.post('http://172.16.2.209:3000/api/AddPartList?access_token=xxxxxxxxxxxx',{headers: headers},data)

但我一直收到错误Response with status: 422 Unprocessable Entity for URL

我想做的curl命令是:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "$class": "org.blockaviation.AddPartList", \ 
   "partListId": "1", \ 
   "partList": "wing,wheel", \ 
   "plane": "resource:org.blockaviation.Plane#Plane_001" \ 
   \ 
 }' 'http://172.16.2.209:3000/api/AddPartList'

有谁知道我做错了什么或如何正确地做到这一点?

2 个答案:

答案 0 :(得分:1)

您应该将标头放在RequestOptions对象中,如下所示:

import { Http, Headers, RequestOptions } from '@angular/http';

private requestOptions = new RequestOptions({
    headers: new Headers({
        'Content-Type': 'application/json'
    })
});

this.http.post(this.url, data, this.requestOptions);

答案 1 :(得分:1)

如果您使用的是>> simplify(frac1 == A/(B*x+C)) ans = TRUE

json-server