I was trying to make a post request to the api route I just created.
In the backend I have something like this
console.log(typeof req.body)
console.log(req.body)
const { firstName, lastName, email, phoneNumber } = req.body
console.log(`Variable Values`, firstName, lastName, email, phoneNumber)
Here I am getting typeof as String and body as this
{
firstName: "Varun",
lastName: "Bindal",
email: "iva#gmail.com",
phoneNumber: "+91-8888"
}
What I want is that the typeof to be object so I can de-structure it, How can I make a request from postman in this case (I don't want use JSON.parse)
Click the "Text" beside it will show you a dropdown. Just choose "JSON" instead of "Text"
Choose the JSON option as shown in the picture.
You should change the type of body from raw text to JSON (application/json) by clicking on the text button right next to your GraphQL option.
Your object body is of type text. Change it to JSON using the little dropdown and the POST request will work.
Cheers!
Why don't you want to use JSON.parse?
It's important to know that JSON and a javascript object are two different things.
JSON is a data format format that can be used in various environments while a javascript object is a data structure/concept in javascript.
When making a HTTP request you can send data via a few different methods. A few prominent ones being XML, Binary and JSON (They all will be represented as text, even binary).
Since you're building a API with javascript I would recommended that you use JSON in your requests and responses. JSON has also somewhat become the "standard" for APIs these days. It's also very easy to parse JSON to javascript objects and the other way around.
Please note that you maybe also need to tell postman to set the Content Type Header to application/json. You also would need to change your body to be actual valid JSON:
{
"firstName": "Varun",
"lastName": "Bindal",
"email": "iva#gmail.com",
"phoneNumber": "+91-8888"
}
I can recommend that you read the following article explaining what JSON is and how you use it: https://www.w3schools.com/js/js_json_intro.asp
Related
I have a Javascript JSON object
const student = { name: "bob", age: 7, grade: 6 }
and I can pass it to my Web API using axios POST command by
JSON.stringify(student)
and I can build my student object by looping an array and passing in a value such as
let studentArr= []
const student = { name: studentArr[i]["name"], age: studentArr[i]["age"], grade: studentArr[i]["grade"}
I'm using i in the example as the index because it could have 100 students. As long as I pass in only one value for i, everything works fine. My question is how can I make it into a multi-element JSON object from my array. I've been spoiled by Newtonsoft.Json where I can pull data from a SQL database and create a JSON object. If I just JSON.stringify(studentARR) is shows empty. I want to pass to the Web API all of the students on one post so the Web API can make a document and download it back.
I seen many different ways of trying to accomplish this and the methods seem to change over time. Thanks for the help
Why do you have to JSON.stringify if you are using axios. The declaration of axios.post accepts URL as first parameter and the data which is formdata or the JSON object as second parameter
I'm pretty sure that you might not need to use JSON.stringify if you are using axios to post to a Web API
Example of using Axios post method https://axios-http.com/docs/post_example
Description of the problem
I will not write any codes as much of my problem is something linked with the knowledge to interpret some data. I am doing a project and had to use a nonsql database to store data the sampled information from a microcontroller. The chosen db was mondodb. I wrote all the code that stores the info and now i want to exhibit the date on a html page. The problem is that when i do the request using restapi to the mongodb, the json that was stored there comes in the extened json format, i.e.:
"_id": {
"$oid": "6230d05dcf81542c5aabc30b"
},
"sensor": {
"$numberDouble": "1"
}
But it should have come as the data is stored in the db:
"{
_id": "6230d05dcf81542c5aabc30b",
"sensor": 1.0
}
As you can see, the the json comes with extra information linked to the type of the variable that is stored. But i don't really know how to use that information in javascript. I would just read it for example as json.sensor.$numberDouble if i wanted to get the information about the sensor instead of json.sensor if the json was in the normal way. I don't see much of an use to the extended version. Is something i am doing wrong?
Make sure, that you Parse API response, like-
response.json()
It looks like you're getting back an EJSON response. If you want to send back standard JSON response then that should be set as the Content-Type on the server response.
function(req, res) {
data = { ... }; // Your data here
res.setHeader("Content-Type", "application/json");
res.setBody(JSON.stringify(data));
}
If you don't have control over the server side, then convert the EJSON back to plain JSON using the meteor EJSON library.
I'm currently working with a bearer token I need to get some data out of to continue a user journey. I found the following code that does the decoding for me
var jsonData = JSON.parse(responseBody);
const payload = jsonData.Result.AccessToken.split('.')[1]; // Assuming the JWT is in id_token
const parsed = JSON.parse(atob(payload));
It uses atob sandboxed script to decode base 64 encoded payload, when decoded the payload looks a little like this:
{
"http://mytestsite.uk/roles": [
"user"
],
"http://mytestsite.uk/id": "8d2c3de9-4fa2-a58e-68109d977",
"http://mytestsite.uk/email": "tst3#mytestsite.uk",
"sub": "500b416c8942bb0069b98a3c",
"aud": "api.tst.mytestsite.uk",
"iat": 1615544685,
"exp": 1615631085
}
Normally, I would reference the payload body item I want (e.g. sub) like so:
pm.environment.set('sub', parsed.sub);
However, I want to reference "http://mytestsite.uk/id" and am uncertain how to do it.
parsed.http://mytestsite.uk/id
The above causes the script to error and I can't encode it, as that doesn't remove the dots, I obviously can't pass it as a string - I guess I'm unclear on how to correctly pass this name to reference the JSON object correctly.
You should be able to reference it this way:
pm.environment.set('id', parsed['http://mytestsite.uk/id'])
Im just trying to send a json object to sqs queue by using aws-sdk npm package.
const sqsMessage = {
MessageAttributes: {
"data": {
DataType: "String",
StringValue: payload.data
}
},
MessageBody: JSON.stringify(payload),
QueueUrl: queueUrl
If i pass json object in data attribute it tells it expected String, if i add Json then it also throws an error about type, does anyone know a workaround or what to use to get the json?
Some useful links i've found,
1)https://blog.chrismitchellonline.com/posts/aws-sqs-message-with-attributes/
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-send-message.html
Would really appreciate some guidance on this
Can you share the error message that you're receiving. And can you show us an example of payload that you're constructing? Basically, the allowable values for dataType are found here: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_MessageAttributeValue.html
Specifically, this part: "Amazon SQS supports the following logical data types: String, Number, and Binary. For the Number data type, you must use StringValue."
So when it comes to constructing the value for your key MessageBody, you want to use JSON. So the type must be string and your value, it has to be valid JSON that's really a string and the inner quotes escaped. So for example, something like this:
"{\"foo\": \"bar\"}"
I ran into this same issue while writing unit tests in goLang and it works. I know this is 11 months later, but hopefully this helps you or someone else in the future :)
I'm writing an Angular2 web application, and I want bidirectional communication with the server, so the perfect solution is with websockets.
Mozilla fundation teaches to use JSON.stringify(msg) to parse a string that represents a JSON message, both for sending and receiving data with websocket. This leads to some thoughts on injection. For example, suppose the following JSON document being transfered from the server to the client's machine:
{
message{
from: "paul",
text:"hello!",
date:"01/01/2017"
}
}
Now suppose paul changes his name to "paul\" and sends the following text:
,text: alert('xss'),something:"
then the following JSON received by the client would be:
{
message{
from: "paul\",
text:",text: alert('xss'),something:"",
date:"01/01/2017"
}
}
which is, in fact, the following JSON (just rearranged):
{
message{
from: "paul\",text:",
text: alert('xss'),
something:"",
date:"01/01/2017"
}
}
which could cause problems, depending on what is done with this JSON, I guess. Is there some way to prevent these things, or is the JSON.stringfy technique safe because when inserting data from the JSON into the DOM, the text attribute would be undefined because alert('xss') is not a string (or does not return anything)?
Also, see that if there's no problem with calling a function like alert(), we were able to set the something variable to anything we wanted, this is for sure risky.