我正在尝试使用angular的HTTP请求来使用Twilio发送文本消息。我可以在邮递员中使用身体的表格数据来做到这一点 postman body
但是,当我尝试将表格数据添加到angular的http请求中时,似乎Twilio的api无法正确读取属性名称。我所有的搜索都告诉我,我只需要将它们附加到formData对象,并将其作为帖子中的第二个参数传递,但这是行不通的。
我知道我的请求已到达正确的端点,因为我以前收到了401未经授权的响应,但已使用btoa()修复了该问题。
这是代码
sendSMS(sendTo: string){
//actual phone numbers replaced (they worked with postman)
const to: string = '+15555555555';
const from: string = '+15555555555';
const body: string = 'Image Uploaded';
const username: string = 'username';
const password: string = 'password';
var form = new FormData();
form.append('To', btoa(to));
form.append('From', btoa(from));
form.append('Body', btoa(body));
const headers = new Headers({});
headers.append("Authorization", "Basic " + btoa(username + ":" + password));
headers.append("Content-Type", "multipart/form-data");
const options = new RequestOptions({headers});
return this.http.post(this.smsURL,form, options);
}
答案 0 :(得分:0)
我最终弄清楚了
#include <iostream>
#include <string>
#include <libssh.h>
#include <stdlib.h>
#include <fstream>
#include <stdio.h>
#include <Windows.h>
#include <fcntl.h>
#include <sstream>
#include <io.h>
using namespace std;
static int fetch_files(ssh_session session){
int size;
//char buffer[16384];
int mode;
int rc;
//creating scp session to read from remote host for this file
ssh_scp scp = ssh_scp_new(session, SSH_SCP_READ | SSH_SCP_RECURSIVE, "/home/snaps.jpg");
//executing the init
rc = ssh_scp_init(scp);
//pulling from request object
rc = ssh_scp_pull_request(scp);
if (rc != SSH_SCP_REQUEST_NEWFILE)
{
std::cout << "Error receiving information about file: rc != SSH_SCP_REQUEST_NEWFILE";
return -3;
}
else {
//trying to download the file from the created scp object
int t_filesize = ssh_scp_request_get_size(scp);
cout << "filesize is = " << t_filesize;
string t_filename = strdup(ssh_scp_request_get_filename(scp));
cout << "File name read is = " << t_filename;
int t_filemode = ssh_scp_request_get_permissions(scp);
cout << "File mode is = " << t_filemode;
//fprintf(stderr, "Receiving file %s, size %d, permisssions 0%o\n", t_filename, t_filesize, t_filemode);
FILE *fptr = fopen("file.jpg", "w");
if (NULL == fptr)
{
fprintf(stderr, "Error opening local file: %s, error %s\n", t_filename, strerror(errno));
ssh_scp_deny_request(scp, "Unable to open local file");
}
ssh_scp_accept_request(scp);
while (rc >= 0)
{ //reading and storing into buffer to write on local file system
rc = ssh_scp_read(scp, buffer, sizeof(buffer));
cout << "Buffer = " << buffer;
//cout << "RC = " << rc << " and SSH_ERROR is = " << SSH_ERROR;
if (rc == SSH_ERROR)
{
fprintf(stderr, "Error receiving file data: %s\n", ssh_get_error(session));
fclose(fptr);
ssh_scp_close(scp);
ssh_scp_free(scp);
return rc;
}
//fprintf(stderr, "Bytes received = %d\n", rc);
if (fwrite(buffer, rc, 1, fptr) != t_filesize)
{
fprintf(stderr, "Error writing file data: %s\n", strerror(errno));
fclose(fptr);
ssh_scp_close(scp);
ssh_scp_free(scp);
return rc;
}
}
cout << "Buffer is = " << buffer;
fclose(fptr);
}
诀窍是这样格式化主体:
sendSMS(sendTo: string){
const to: string = '+' + sendTo;
const from: string = '+fromNumber';
const body: string = 'Image Uploaded';
const username: string = 'username';
const password: string = 'password';
var rbody = `To=+${to}&From=${from}&Body=Image Uploaded`;
const headers = new Headers({});
headers.append("Authorization", "Basic " + btoa(username + ":" + password));
headers.append("Content-Type", "application/x-www-form-urlencoded");
const options = new RequestOptions({headers});
return this.http.post(this.smsURL,rbody, options);
}
并将标题中的内容类型设置为:
body= 'attribute1=value&attribute2=value2'
答案 1 :(得分:0)
这里是Twilio开发人员的传播者。
您似乎正在尝试从客户端向Twilio API发出请求。这里的问题是您将Twilio凭据存储在任何人都可以读取的前端代码中。这意味着他们可以获取您的凭据并滥用您的帐户。
在Angular中使用Twilio发送SMS消息的推荐方法是构建一个服务器端终结点,您可以控制该终结点来存储凭据并发送消息。然后,您可以将消息的详细信息从客户端发送到服务器,服务器将其凭证发送到API。
我有一个example app that sends SMS messages with Angular and an Express backend。它使用message service to send the details to the server和this code to send messages from the server。
作为记录,when you create a FormData
object, it makes the request as a multipart/form-data
。因此,当您使用字符串代替它时,它开始起作用。