0

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?

1 Answer 1

1

use request.write(JSON.stringify($scope.jsonToSend)) then when getting response parse it to json object

Sign up to request clarification or add additional context in comments.

1 Comment

after i've to use JSON.parse(jsonRespond);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.