Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1474877
  • 博文数量: 931
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 10198
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-08 12:28
个人简介

测试

文章分类

全部博文(931)

文章存档

2020年(134)

2019年(792)

2018年(5)

我的朋友

分类: 架构设计与优化

2020-01-15 10:56:54

var request = require('request');

var url = "https://jerry.c4c.saphybriscloud.cn/sap/c4c/odata/cust/v1/zjerrysalesorder";

var getTokenOptions = {
        url: url,
        method: "GET",
        json:true,
        headers: {
            "content-type": "application/json",
            'Authorization': "Basic ABCDE00ZXN0MQ==",
            "x-csrf-token" :"fetch"
        }
};

function getToken() {
  return new Promise(function(resolve,reject){
      var requestC = request.defaults({jar: true});
      console.log("Step1: get csrf token via url: " + url );

      requestC(getTokenOptions,function(error,response,body){
       var csrfToken = response.headers['x-csrf-token'];
       if(!csrfToken){
          reject({message:"token fetch error"});
          return;
       }
       console.log("Step1: csrf token got: " + csrfToken);
       resolve(csrfToken);
      }); 
     });
}

function createSalesOrder(token, oPostData){

	return new Promise(function(resolve, reject){
		var requestC = request.defaults({jar: true});
        var createOptions = {
              url: "https://jerry.c4c.saphybriscloud.cn/sap/c4c/odata/cust/v1/zjerrysalesorder/CustomerQuoteCollection",
              method: "POST",
              json:true,
              headers: {
                  "content-type": "application/json",
                  'x-csrf-token': token
              },
              body:oPostData
        };
        requestC(createOptions,function(error,response,data){
            if(error){
                reject(error.message);
            }else {
               resolve(data);
            }
        });
	});
}


getToken().then(function(token) {

  var payload = {
    "Name": "Jerry Test 2019-1-12 19:52PM",
    "TypeCode": "2059",
    "BuyerID":"Jerry open ID",
    "PartyID":"60102",
    "CustomerQuoteText":[{
      "Text": "test comment by Jerry Wang",
      "TypeCode": "10024"
    }],
    "CustomerQuoteItem":[{
      "ProductID": "1042416"
    }],
    "CustomerQuoteItemProposal":[{
      "ProductUUID": "00163E72-09C6-1EE8-BBDC-AC5F0CB0D795",
      "Quantity": "1",
      "unitCode": "EA"
    }]
  };
  createSalesOrder(token, payload).then(function(oData){
    console.log("created Sales Order by OData: " + oData.d.results.ID);
  });
});
阅读(1433) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~