Reputation: 1622
I'm using request module to send data to a server.
I wrote this:
...
var httpOptions = {
host: localStorage.host,
path: localStorage.path,
port: localStorage.port,
method: 'POST',
body: $scope.jsonToSend,
json: true,
headers: {
'Content-Type': 'application/json',
}
};
...
try {
var request = http.request(httpOptions, httpCallback);
request.on('error', function(err) {
$scope.hasHttpError = true;
$scope.httpErrorMessage = 'NETWORK error '+err;
$scope.sending = false;
$scope.$apply();
});
request.write($scope.jsonToSend);
request.end();
} catch (e) {
$scope.hasHttpError = true;
$scope.httpErrorMessage = 'NETWORK error '+e.toString();
$scope.sending = false;
$scope.$apply();
} finally {
}
But I'm getting this error:
Error: NETWORK error TypeError: First argument must be a string or Buffer
Naturally it's referred to request.write
function. My question is, how can I can send JSON to a server?
Upvotes: 0
Views: 47
Reputation: 982
use request.write(JSON.stringify($scope.jsonToSend))
then when getting response parse it to json object
Upvotes: 1