I have been working on one of my projects using node and ng4
when I came across using res.json to send an array back to Angular 4 front end, like res.json(arrayResult). Surprisingly, I cannot use JSON.parse(arrayResult) after Angular receives it because it throws an error saying unexpected end of input. I however can access all the data through result[i], just like any normal array.
I don't quite understand why res.json() does not send my array as a string to the front end. Is there any internal conversion involved? Why I could access the content through index without even parsing it or doing any conversion with it?
The server indeed send your json data as a string. Additionally to the string, the server passes Content-Type header to your client which tells your client what kind of data you received.
So if the Content-Type was text/html your client would think that he received an HTML file.
In your case, res.json using Content-Type: application/json which tells the client that the string that he got is actually a json object, so no need for you to use JSON.parse.
You can see the Content-Type under the response headers property.
Related
I'm using postman to test calling a rest service endpoint.
I'm trying to parse the JSON return content but it throws an error because the response body has more than just JSON.
This is how I parse it in my postman test script:
var jsonData = JSON.parse(responseBody);
Here is the response body:
--13398550-b6ea-4731-a8ee-4b2ad24c3cfe
Content-Type: application/json; charset=utf-8
//this is the actual content I want to parse --->
{"id":"123456","value":"the_value"}
--13398550-b6ea-4731-a8ee-4b2ad24c3cfe--
When I try to parse it, I get the following error (in postman)
There was an error in evaluating the test script: SyntaxError:
Unexpected number in JSON at position 3
Obviously because the content being parsed is not just JSON
Is this something special that the api is doing? Or am I just parsing it incorrectly?
NOTE: I'm not including details of the rest service function. If the cause of this issue is something that is being done by the service itself, then that is enough of an answer for me to perhaps ask another question or do some further investigation. The purpose of this question is to ask whether this is something special being done in HTTP, or if it's the service.
Edit:
I managed to see the server side code and it is indeed manually building the response with boundaries identified by a GUID. I'll have to manually parse the response
The server is not emitting straight up application/json, it's packed in a multipart mime envelope.
Whether or not it's doing that correctly depends on the response headers. If you didn't expect a multipart response, but a simple JSON response, then I'd say yes: it's something you need to fix server-side.
Let's say I'm making the http request below, to update some record in a mongoDB database:
PUT
http://dev.mycompany.co/ping
{"id":4432, "name":"Jane Doe", "products":[ {"id":287}, {"id":434} ] }
Notice that there is an array of two objects inside the "products" property of the JSON above. I have data that is being sent in this general format, both through Fiddler and through a ruby script I've written.
When sent through Fiddler, my data in the JSON is correctly parsed and updated into my database. When sent through my ruby script, the data in the JSON is not parsed correctly. I've been trying to figure out why this is. Now, I'm wondering how data is actually sent when a JSON is sent along with an http request. Can someone point me in the right direction?
After asking a few other questions on SO and doing a bunch more research, I've arrived at the following conclusions.
Q: How does JSON data get sent in an http request?
A: It depends on how you are sending the JSON data in the request (what is the content-type)
I've encountered two ways that JSON data is sent.
Sent using content-type application/json
With this content-type, JSON data is sent literally as-is. The literal JSON data is stored as a string and sent with the request. If your JSON is complex, with nested objects and arrays and such, this is probably what you want. For a working example of sending nested JSONs using Ruby's Net/HTTP, see the answer on this SO question I asked.
Sent using content-type x-www-form-urlencoded
This is how Ruby's Net/HTTP requests typically get sent out. The form of these requests is something like: id=343?entity=Microsoft?foo=bar. This content-type is fine until you have a complex JSON.
I have the current test I want to run:
var request = require('supertest');
it('should be malformed json', function(done) {
request(config.base)
.post('/authenticate')
.send('{"project":{"description":\'test"}}')
.set('Authorization', 'Bearer ' + config.token)
.expect('Content-Type', /json/)
.expect(status.BAD_REQUEST);
});
However, supertest seems to validate it, and it just sends '{}' in the body. Any idea how I can get around this?
You are passing a string to the send method, so it will be sent to the server as is (you could write anything there!).
Once the server receives the string, it parses the JSON and it finds the error. What happens next depend on how the server application is configured (or the framework you're using). It is possible that in this case the server just ignores any malformed JSON input, and thus it's like you called /authenticate without any input.
TL;DR: send() just sends any string you pass it. If you want to raise an error, you need to modify the server, and not the test suite.
EDIT
I did some more digging. First of all, as I said before, send() indeed leaves strings as is. You can see it from the code: https://github.com/visionmedia/superagent/blob/master/lib/client.js#L778
Thus said, you did not tell the server that the request body was in JSON format, so what you're saying is interpreted as "text/plain" and not parsed. To pass a manual JSON string to send() you also need to specify the content-type of the request:
request(config.base)
.post('/authenticate')
.type('json')
.send('{"project":{"description":\'test"}}')
//...
I'm using Cordys BOP and I have a REST web service that returns JSON format. I have a test service that I use, that returns XML and this works fine so I know that my HTTP connector works right. When I try to test my JSON service using the Test Web Service Operation UI, I get an error that says Invalid XML response.
How do I tell Cordys to expect a JSON response instead of an XML?
Is there a way to somehow wrap a JSON in XML through Cordys?
You probably need to change the header:
Content-type: application/json
I am using jQuery to post JSON data to a tomcat server and the server is handling the JSON array data perfectly! It is as though I passed key=value request parameters along with the URL.
So why am I posting this? I would like to know how the server treats JSON in the body of a request and how the data ends up being interpreted as request parameters. I have Googled my a** off and all I find is how the server sends JSON back to the client.
$.ajax() converts JSON data into key-value pairs (querystring style) by default. You need to set { processData : false } in the AJAX request to keep it as raw JSON.