簡體   English   中英

http模塊(帶有browserify的node.js)未使用PATCH方法寫入請求主體

[英]http module (of node.js with browserify) doesn't write request body with method PATCH

我一直在開發一個Web客戶端以與REST API服務器進行交互,並且想使用PATCH方法。

盡管我嘗試將請求正文寫入PATCH的請求中,但發現該正文仍然為空。 PUT或POST的工作原理相同。

我可以改用PUT,但是有人知道我對http模塊的使用是否錯誤嗎?

先感謝您。

var http = require('http');

module.exports = {

  patch: function(path, data, done, fail){
    var jsonData = JSON.stringify(data);
    var options = {
      headers: {
        'Content-Type':'application/json;charset=UTF-8',
        'Content-Length':jsonData.length,
      }
    };
    var req = this.request(path, "PATCH", done, fail, options);

    // THIS CODE DOESN'T WRITE jsonData INTO REQUEST BODY
    req.write(jsonData);
    req.end();
  },

  request: function(path, method, done = () => {}, fail = () => {}, options = { headers: {} } ){
    options.path = path;
    options.method = method;
    return http.request(options, function(res){
      var body = '';
      res.setEncoding('utf8');
      res.on("data", function(chunk){
        body += chunk;
      });
      res.on("end", function(){
        // process after receiving data from server
      });
    }).on("error", function(e) {
      // process after receiving error
    });
  }
}

我有與此代碼相同的問題:

function patch(path, entity){
    var deferred = q.defer();     
    var escapedPath = escapeSpaces(path);
    var stringEntity = JSON.stringify(entity);
    AuthProvider.retrieveToken().done(function (authToken){
        var options = {
            host: resourceApiId,
            path: "/api/data/v8.0/"+escapedPath,
            method: 'PATCH',
            headers: {
                'OData-MaxVersion': '4.0',
                "OData-Version": '4.0',
                "Content-Type": "application/json; charset=utf-8",
                "Authorization": 'Bearer '+ authToken,
                "Accept": "application/json"
            }       
        };  

        var reqToCrm = https.request(options, function(resFromCrm){
            var body = '';
            resFromCrm.on('data', function(d) {
                body += d;
            });
            resFromCrm.on('end', function() {
                try{
                    var parsed = JSON.parse(body);
                    if(parsed.error){
                        var errorString = "webApiRequest.js/patch: An error returned from CRM: " + parsed.error.message + "\n"
                        +"Body Returned from CRM: " + body;
                        console.log(errorString);
                        deferred.reject(errorString);
                    }
                    deferred.resolve(parsed);
                }
                catch (error){
                    var errorString = "Error parsing the response JSON from CRM.\n"
                    +"Parse Error: " + error.message + "\n"
                    +"Response received: "+ body;
                    console.log(errorString);
                    deferred.reject(errorString);
                }

            });            
        });

        reqToCrm.on('error', function(error) {
            console.log(error.message);
            deferred.reject("webApiRequest.js/post: Error returned on 'PATCH' against CRM \n" + error.message);
        });

        reqToCrm.write(stringEntity);
        reqToCrm.end(); 
    });

    return deferred.promise;
}

我也已經嘗試過發出POST請求並設置'X-HTTP-Method-Override':'PATCH。 也沒有工作。 我不能使用任何其他Request方法,因為Dynamics CRM禁止使用它。

我最終對node.js使用了“請求”庫。 以下代碼有效:

var request = require("request");
(...)

function patch(path, entity){
    var deferred = q.defer();     
    var escapedPath = escapeSpaces(path);
    AuthProvider.retrieveToken().done(function (authToken){
        var headers ={
            'OData-MaxVersion': '4.0',
            'OData-Version': '4.0',
            'Content-Type': 'application/json; charset=utf-8',
            'Authorization': 'Bearer '+ authToken,
            'Accept': 'application/json'     
        }

        var options = {
            url: "https://"+resourceApiId+"/api/data/v8.0/"+escapedPath,
            method: 'PATCH',
            headers: {
                'OData-MaxVersion': '4.0',
                "OData-Version": '4.0',
                "Content-Type": "application/json; charset=utf-8",
                "Authorization": 'Bearer '+ authToken,
                "Accept": "application/json"                
            },
            json: entity      
        };  

        request(options, function(error, response, body){
                if (!error) {
                    console.log("patchrequest statuscode: "+response.statusCode )
                    deferred.resolve(true);
                }
                deferred.reject(error);
            }
        );
    });

    return deferred.promise;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM