【问题标题】:Simple but struggling to convert POST request from using URL params to request body简单但努力将 POST 请求从使用 URL 参数转换为请求正文
【发布时间】:2017-05-08 20:12:15
【问题描述】:

我使用 URL 参数(有效)以下列方式发出 POST 请求:

var PAYLOAD = `
  <myxmlcontent>
    <attribute name="id">1</attribute>
    <attribute name="FullName">Joe Bloggs</attribute>
  </myxmlcontent>
`

var URL = 'http://www.somewhere.com/integration?apiKey=company&apiToken=123&payload=' + PAYLOAD;

client.request({
  url: URL,
  type: 'POST',
  contentType: 'application/xml'
}).then(
  function(data) {
    console.log(data);
  }
);

但我希望将有效负载数据放入请求正文中。

这是正确的方法吗?我不确定,但到目前为止我的尝试被证明是不成功的:

var PAYLOAD = `
  <myxmlcontent>
    <attribute name="id">1</attribute>
    <attribute name="FullName">Joe Bloggs</attribute>
  </myxmlcontent>
`

client.request({
  url: 'http://www.somewhere.com/integration',
  type: 'POST',
  contentType: 'application/xml',
  headers: {
    apiKey: 'company',
    apiToken: '123'
  },
  dataType: 'xml',
  data: 'data=' + JSON.stringify(PAYLOAD)
}).then(
  function(data) {
    console.log(data);
  }
);

我目前正在构建客户端 Zendesk 应用程序。

【问题讨论】:

  • 是的,这基本上是对的,您只是没有对参数值进行编码。并且您的内容类型与您发送的内容不匹配。一个参数字符串可能包含一个 xml 值,但它仍然是一个参数字符串,而不是 xml。
  • 嗨,凯文,感谢您的帮助
  • 如果我理解正确,结果我对代码做了如下修改。请求前:PAYLOAD = encodeURIComponent(PAYLOAD) 请求正文中:dataType: 'text', data: 'data=' + JSON.stringify(PAYLOAD )。不幸的是,我仍然收到 500 条回复。
  • 服务器希望你给它什么?
  • 我正在阅读与 API 相关的文档。 入站 API 中的负载请求参数是 XML。 我不确定这有多值得注意:如果集成是在 Web 服务模式下,**** 和客户端应用程序之间的消息就结束了HTTP/HTTPS。所有 API 都以键值对样式(不是 REST 样式)接受输入,并以 XML 格式响应客户端。身份验证是通过交换在 **** 以及客户应用程序中维护的登录 ID/密码组合。

标签: javascript xml api post zendesk


【解决方案1】:

首先,您必须确保端点通过 POST 接受数据,否则即使您正确发送数据也会失败,其次,如果您想以 url 编码的形式发送数据,则需要更改 @ 987654321@ 到 application/x-www-form-urlencoded 并将正文作为 url 编码的字符串或使用 FormData 对象(如果它在您的框架中可用)发送,例如:

var myData = new FormData();
myData.append("payload", encodeURI(PAYLOAD));

client.request({
  url: 'http://www.somewhere.com/integration',
  type: 'POST',
  contentType: 'application/x-www-form-urlencoded',
  headers: {
    apiKey: 'company',
    apiToken: '123'
  },
  dataType: 'xml',
  data: myData
}).then(
  function(data) {
    console.log(data);
  }
);

别忘了对有效载荷的内容进行编码。如果您的端点只接受 xml 编码的字符串,那么您必须按原样发送字符串,只需确保指定正确的contentType,在这种情况下为application/xmltext/xml

【讨论】:

  • 感谢 arielnmz 帮助我了解更多相关信息。我会看看我是否有任何成功。感谢您抽出宝贵时间为其他人 arielnmz 和 @Kevin_B 提供指导
【解决方案2】:

已解决。这是我必须做的(谢谢):

var PAYLOAD = `
  <myxmlcontent>
    <attribute name="id">1</attribute>
    <attribute name="FullName">Joe Bloggs</attribute>
  </myxmlcontent>
`

var URL = 'http://www.somewhere.com/integration';

client.request({
  url: URL,
  type: 'POST',
  contentType: 'application/x-www-form-urlencoded',
  dataType: 'xml',
  data: {
    apiKey: 'company',
    apiToken: '123',
    payload: PAYLOAD
  }
}).then(
  function(data) {
    console.log(data);
  }
);

有用的文章:How are parameters sent in an HTTP POST request?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2015-04-16
    • 1970-01-01
    相关资源
    最近更新 更多