我有一个 ionic 3 项目,我正在尝试发布请求。到目前为止,我一直在使用以下运行良好的代码:
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import {HttpParams} from '@angular/common/http';
import { Observable, Subscription } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
@Injectable()
export class Need4CarGateway {
token = "";
availabilityPeriod="00:00";
private sub: Subscription;
constructor(public http: Http, private logger:Logger,private utils:Utils) {
}
myPostRequest(){
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded' );
headers.append('Authorization', 'Bearer '+mytoken);
headers.append('access_token', ACCESSTOKEN );
let options = new RequestOptions({ headers: headers });
let body = [
{key: 'first_name', value: firstNameVariable},
{key: 'last_name', value: lastNameVariable}
].map(x => `${encodeURI(x.key)}=${encodeURI(x.value)}`).join('&');
return this.http.post(myurl, body, options)
.retry(NUM_HTTP_RETRIES)
.map((res: Response) => res.json())
.toPromise();
}
}
但是,当我尝试将 base64 图像传递给我的参数时遇到问题,即
myPostRequest(){
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded' );
headers.append('Authorization', 'Bearer '+mytoken);
headers.append('access_token', ACCESSTOKEN );
let options = new RequestOptions({ headers: headers });
let body = [
{key: 'first_name', value: firstNameVariable},
{key: 'last_name', value: lastNameVariable},
{key: 'myIdFile', value: my64baseImgString},
].map(x => `${encodeURI(x.key)}=${encodeURI(x.value)}`).join('&');
return this.http.post(myurl, body, options)
.retry(NUM_HTTP_RETRIES)
.map((res: Response) => res.json())
.toPromise();
}
在那种情况下,图像到达服务器已损坏。我不确定主体变量是否应采用 first_name=AName&last_name=LastName&myIdFile=64baseImg 形式,因为字符串很长。
我一直在四处寻找另一种传递 body 参数的方法。我试着把它写成 json 对象,即
let body = {
first_name:firstNameVariable,
last_name:lastNameVariable,
myIdFile:my64baseImgString
};
或作为 HttpParams,即
let body = new HttpParams();
body.append('first_name',firstNameVariable);
body.append('last_name',lastNameVariable);
body.append('myIdFile',my64baseImgString);
我环顾四周,似乎 body 变量需要是一个对象,但在我的情况下,它并没有按预期工作。如果我把它作为一个对象,我一直有 http 错误。在 Postman 中,如果我按照它要求的方式放置 header 和正文参数,即使用键和值,一切正常。
我做错了什么?
谢谢
最佳答案
不确定这是否是最正确的答案,但它对我有用。实际上你可以发送一个 json 对象,即
let body = {
first_name:firstNameVariable,
last_name:lastNameVariable,
myIdFile:my64baseImgString
};
如果在 header 请求中提到内容是一个 json 对象,即
myPostRequest(){
var headers = new Headers();
headers.append('Content-Type', 'application/json' );
//instead of
//headers.append('Content-Type', 'application/x-www-form-urlencoded' );
headers.append('Authorization', 'Bearer '+mytoken);
headers.append('access_token', ACCESSTOKEN );
let options = new RequestOptions({ headers: headers });
let body = {
first_name:firstNameVariable,
last_name:lastNameVariable,
myIdFile:my64baseImgString
};
return this.http.post(myurl, body, options)
.retry(NUM_HTTP_RETRIES)
.map((res: Response) => res.json())
.toPromise();
}
但是服务器需要接受 Json 对象并允许 CORS(跨源资源共享)。
关于angular - Ionic 3 项目和 base64 图像中 http post 中的数据参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52572707/