通过请求发送XML和XMLHttpRequest

时间:2018-02-16 13:49:14

标签: javascript api request

我尝试将XML数据发送到API,但我没有得到任何回复,只有<html></html> 问题是,如果我做错了什么或目标服务器......

我可以使用请求将XML作为字符串发送吗? 像:

var xml = '' +
  '<?xml version="1.0" encoding="UTF-8"?>' +
  '<Request version="1.0">' +
  '<Header>'+
  '<Security sender="'+ 123456 +'" />' +
  '</Header>' +
  ' <Transaction mode="LIVE">' +
  '     <User login="'+ login +'" pwd="'+ pwd +'"/>' +
  ' </Transaction>' +
  '</Request> '

// request type 1  
    request.post({
      headers: {'content-type' : 'application/x-www-form-urlencoded', 'charset' : 'UTF-8'},
      url:     'https://test.url.io',
      body:    xml
    }, function(error, response, body){
      if (error) { return console.log(err); }
      console.log(body);
    });

使用XMLHttpRequest

时会产生<html></html>相同的结果
// request type 2
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","https://test.url.io",true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.setRequestHeader('charset', 'UTF-8');
xmlhttp.send(escape(xml));

这里是API描述:

请求编码 对于所有请求,请求标头必须包含Content-Type / charset参数 charset编码设置为“UTF-8”。实际内容类型可能不同;决定性的信息是charset 平台文档 - XML Integrator 7 值。

因此,必须使用UTF-8字符集对所有请求数据进行编码。 对于XML数据,请使用以下内容类型:application / x-www-form-urlencoded; charset = UTF-8

实施例: 通过PHP / cURL进行集成:http://php.net/manual/de/book.curl.php

$ch = curl_init();
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
 ));

// attach parameter
curl_exec($ch);

通过Java集成:

 UrlRequest req = new UrlRequest(UrlRequest.POST, CORE_URL );
    // attach parameter to request
    req.addHeaderParam("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    req.send();

0 个答案:

没有答案