2014-01-27 12 views
20

Ich benutze das Request-Modul in Node.js, um eine Put-Anfrage zu machen. Mein Code sieht wie folgt ausNode.js - PUT mit Modul 'request'

var request = require('request'); 
var data = {foo: "bar", woo: "car"}; 

request({ 
    method: 'PUT', 
    uri: myURL, 
    multipart: [{ 
     'content-type':'application/json', 
     body: JSON.stringify(data) 
    }] 
}, function(error, request, body){ 
    console.log(body); 
}); 

Als ich das laufen bekomme ich einen Fehler:

"Nicht unterstützte Inhalte mit Typ: application/json"

Antwort

27

Probieren Sie es wie folgt aus:

request({ url: url, method: 'PUT', json: {foo: "bar", woo: "car"}}, callback)

+6

Das funktionierte wie ein Charme! Ich frage mich, warum die Dokumentation nicht so einfach war? – MonsterWimp757

+0

Und wo würde ich Header hinzufügen? –

+0

Header ist nur ein geschachtelter JSON in den Optionen JSON. – labyrinth

Verwandte Themen