Request: Senden Sie in der Anfrage einen JSON-Nutzlasttext

Erstellt am 6. Aug. 2015  ·  7Kommentare  ·  Quelle: request/request

Hi,

Ich würde gerne wissen, wie ich diese Struktur senden kann:

[
  {
    "sha1": "3722059cc37f7a56db064c1911f063da826cb211",
    "size": 36
  },
  {
    "sha1": "a9993e364706816aba3e25717850c26c9cd0d89d",
    "size": 1
  }
]

In einer PUT-Anfrage.
Ich entwickle einen Cloud-Foundry-Client für Node.js und habe diese Zweifel.

http://apidocs.cloudfoundry.org/214/resource_match/list_all_matching_resources.html

Ich habe diese Bitte:

    var url = this.API_URL + "/v2/resource_match";  
    var headers = {
        'Accept': 'application/json',
        'Authorization': token_type + " " + access_token,
        'Content-Type': 'application/x-www-form-urlencoded'
    };
    return new Promise(function (resolve, reject) {
        request.put({url:url, headers: headers}, function optionalCallback(err, httpResponse, body) {
            if (err) {
                console.error('upload failed:', err);
                return reject(error);
            }
            console.log('Upload successful!  Server responded with:', body);
            return resolve(body);
        });
    });

Aber meine Frage ist, wie man den JSON zur Anfrage hinzufügt.

Was ist der beste Weg?

Vielen Dank im Voraus.

Juan Antonio

Hilfreichster Kommentar

{body:JSON.stringify({object})}

Alle 7 Kommentare

{body:JSON.stringify({object})}

Hi,

Ich habe die Idee getestet, aber ich erhalte die folgende Fehlermeldung:

Error: Error: Argument error, options.body.

Vollständiger Code:

HttpUtils.prototype.DEBUG = function(method,url,headers,qs,body,httpStatusAssert){

    var resources = [
        {
            "sha1": "3722059cc37f7a56db064c1911f063da826cb211",
            "size": 36
        },
        {
            "sha1": "a9993e364706816aba3e25717850c26c9cd0d89d",
            "size": 1
        }];

    var options = {
      method: 'PUT',
      url: url,
      headers: headers,
      body: {body:JSON.stringify(resources)}
    };

    return new Promise(function (resolve, reject) {
        request(options, function (error, response, body) {
            if(error){
                return reject(error);
            }
            console.log(body);
            return resolve(body);
        });
    });
}

In welcher Option muss ich Folgendes anhängen: { body:JSON.stringify ({object})} in der Anfrage?

Ich benutze:

"Anfrage": "^2.45.0"

Nein, nur {body:JSON.stringify({object})} - _string_. Stellen Sie außerdem sicher, dass Sie die neueste Version von request verwenden.

Vielen Dank, es läuft gut!

Bitte beachten Sie, dass sich dies geändert hat und gemäß dem README- Abschnitt in Bezug auf options sollte es stattdessen request({body: <JSON-serializable-object>, json: true, url:...}) sein und nicht das oben erwähnte _stringified_-Objekt, wenn json true ist . Ich fand das verwirrend, als ich Anfragen in meinem Code mischte.

Ich ergänze das, da ich ein ähnliches Problem hatte. Ich musste den Wert von "body" nicht in Optionen mit Objektklammern umschließen. Das war also mein gültiger Code:

var solution = [ {"x": 0, "y": 0}, {"x": 1, "y": 0},
{"x": 2, "y": 0},
{"x": 2, "y": 1},
{"x": 2, "y": 2},
{"x": 3, "y": 2},
{"x": 3, "y": 3}
]
var options = {
method: 'POST',
url: url,
headers: headers,
body: solution
};

rp(options)...
}

Ich verwende die HTTP-Anfrage von nodejs

Ändern
request.write(JSON.stringify(data));
Zu
request.write(JSON.stringify({body: data}));

Es ist Arbeit für mich

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen