2

I use this curl command:

curl -X POST -H "Content-Type: application/json" http://localhost:8081/creditcard -d '{"credit-card":"1234-5678-9101-1121"}'

In my js file, I have this code block to get the credit-card's value:

request.on('data', function(data) {
    var cc = 'credit-card';

    var a = JSON.parse(data.toString());

    console.log(a[cc]);
}

For this I get:

undefined:1
'{credit-card:1234-5678-9101-1121}'
^
SyntaxError: Unexpected token '
    at Object.parse (native)
    at IncomingMessage.<anonymous> (<path>\ccserver.js:32:34)
    at IncomingMessage.emit (events.js:107:17)
    at IncomingMessage.Readable.read (_stream_readable.js:373:10)
    at flow (_stream_readable.js:750:26)
    at resume_ (_stream_readable.js:730:3)
    at _stream_readable.js:717:7
    at process._tickCallback (node.js:355:11)

So I tried to use JSON.stringify as followed:

request.on('data', function(data) {
    var cc = 'credit-card';

    var a = JSON.parse(JSON.stringify(data.toString()));

    console.log(a[cc]);
}

But this is what I get:

undefined
undefined

However, when I try to parse a hard-coded json string, it goes ok:

var jsonString = '{"credit-card":"1234-5678-9101-1121"}';
var a = JSON.parse(jsonString);
console.log(a[cc]);

Result:

1234-5678-9101-1121

What is the correct way to do get the data out of this json?

Please advise Thanks

Leeran Setton
  • 185
  • 1
  • 10
  • why do you call `data.toString()`? Isn't that already a `string`? – smnbbrv Jun 05 '15 at 14:34
  • @simon The [`'data'` `chunk`](https://nodejs.org/api/stream.html#stream_event_data) is by default a `Buffer` unless [`setEncoding()`](https://nodejs.org/api/stream.html#stream_readable_setencoding_encoding) is used. – Jonathan Lonowski Jun 05 '15 at 14:37
  • 1
    It appears the double-quotes are being consumed before the JSON arrives at the server. Which console/terminal/shell are you using to invoke `curl`? – Jonathan Lonowski Jun 05 '15 at 14:40
  • To rephrase what @JonathanLonowski already said, you're asking "*What is the correct way to do get the data out of this json?*" but you should be asking "*What is the correct way to get cURL to send the correct JSON string?*" – apsillers Jun 05 '15 at 14:50
  • @JonathanLonowski - I downloaded curl command for Windows, and I'm running from cmd. – Leeran Setton Jun 05 '15 at 14:50
  • @apsillers - the cURL command can't be changed.. – Leeran Setton Jun 05 '15 at 14:53
  • There's a [comment on a related question](http://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest#comment27632717_7173011) that suggests needing to use double-quotes for the argument and escaping those in the JSON for Windows -- `-d "{\"credit-card\":\"1234-5678-9101-1121\"}"`. Also, [CouchDB cURL Windows Command Line Invalid JSON](http://stackoverflow.com/questions/18314796/couchdb-curl-windows-command-line-invalid-json) – Jonathan Lonowski Jun 05 '15 at 14:53
  • @user478558 I'm not suggested editing `curl` itself, buy trying the revise the format used for the JSON as a command line argument. – Jonathan Lonowski Jun 05 '15 at 14:56
  • @JonathanLonowski if I change the cURL to your suggestion, then it works. But I didn't understand how to revise the format.. – Leeran Setton Jun 05 '15 at 15:39

1 Answers1

1

Try reading from absolute path

curl -X POST
     -H 'Content-Type:application/json'
     -H 'Accept: application/json'
     --data-binary @/full/path/to/test.json
     http://server:port/xyz/abc/blah -v -s

Well, you already have String so all you need to convert it to javascript variable and get using .notation. Suggest to use firebug to see what is in variable.

 obj = JSON.parse(json);
 obj.cc or obj.cc[0] 

should give you what you want.

Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24
  • The steps add content type at request and response. You are getting '' in your response which may be due to content type. Have you tried firebug or F12 option in IE to see values? – Sheetal Mohan Sharma Jun 05 '15 at 14:59