Frisby.JS - unexpected token ( when sending a POST with json - node.js

I'm getting the following error,
Error: Error parsing JSON string: Unexpected token <
Given: <html>
<body>
<p>
<b>400.</b> Invalid json request: numOfDays=1&checkInDate=2013-04-10.
Make sure that key names and text values are quoted with "
</p>
</body>
</html>
Stacktrace:
using the following code:
var frisby = require('frisby');
frisby.create('Ensure proper values in specified keys')
.post('http://localhost:9090/bookRoom', {"numOfDays": 1 , "checkInDate":"2013-04-10"})
.expectJSON({
"checkInDate": "2013-04-10",
"checkOutDate": "2013-04-11",
"totalPrice": 130
})
.toss()
I'm trying to pass this JSON - {"numOfDays": 1 , "checkInDate":"2013-04-10"}
EDIT:
It seems that the JSON being passed is in the following format. How do I get rid of those backslashes?
Handling booking request: "{\"numOfDays\":\"1\",\"checkInDate\":\"2013-04-10\"}"

Author of Frisby.js here. Please update to Frisby v0.8.5 - this is due to a change in the "request" module that Frisby depends on and uses.

I've just run into the same problem but I used different parameters for post. Precisely I added {json: true} as 3rd parameter and after a few minutes of struggling I removed it and it worked.
It looks your case might be the other way around. Try to put that 3rd parameter in the post method call and hope the best.
Related documentation: API docs

Related

Body is not getting parsed in GET method

I am using mockoon for API simulation. I created 2 routes there with method GET and its body contains(responds with) JSON object. I noticed that my express app is not able to parse one of the routes. But the route that has JSON object in body which contains ARRAY is getting parsed. I tested both routes with Express(by console.log) and in chrome browser(I have JSON formatter extension) and it is behaving the same meaning response that does not contain ARRAY is not getting parsed but the response with array is getting parsed(behaving normally). Let me show the screenshots:
Express(by console.log):
With array:
Without array:
Chrome(JSON Formatter extension):
With array(extension is able to parse):
Without array(extension is not able to parse):
I tried adding Header(Content-Type: application/json) to the route in mockoon. But still, I am not aware of what is going on here. Someone please explain
The express code:
const iabs_client = await axios.get(
"http://localhost:3001/iabs-client
);
Here is the route created in Mockoon(without array inside JSON):
P.S mockoon is a program that creates endpoints in localhost, useful for API simulation when developing front-end without having backend yet
The trailing comma after "something" is not valid JSON. Edit your Mockoon body to remove the comma and it should work.

NetSuite SuiteScript 2.0 How to parse content Text in suitescript

i am trying to update a vendor record status field using suitescript 2.0,passing the body in postman tool and is working fine for content JSON but the problem is when i try content as Text its getting error don't know how to read body value in suitescript 2.0.
input body from postman
sample code is
function doPut(context)
{
var obj=JSON.stringify(context.ids);-----here is the error context is empty
// tried JSON.parse also getting undefined
log.debug('str: '+obj);
return obj;
}
If you could provide the exact error message that would be helpful.
But in the mean time a few things that you should verify in your script, if you do not pass application/JSON in header, and your data is object, you need to explicitly parse it into JSON(i.e use JSON.parse() on the request-body), and your response type too should be in the same format i.e your response type should match content-type in the request.
looks like you may have the wrong Content-type. Should be Application-json. If not try Json.parse on the body if you're using text/plain. First step is to always log the context to console or run Object.keys(context) to see what's there. Also make sure doPut is exported as a function

Acces JSON Object in google cloud functions

i'm trying to parse a response in a google cloud function.
The text response is:
{"data":{"Object_Create":{"id":107}}} and stored in var body.
i convert it this way:
var obj= JSON.parse(body)
Then i try to access the object:
var Object_Create= obj.data.Object_Create
and works.
The problem is when i try to get the id field with:
var id= obj.data.Object_Create.id
because it returns ReferenceError: id is not defined
I tried the same code from an online JS editor and it's working without any problems so it seems related to the cloud platform.
Did someone experienced the same problem?
There is no need to parse that response, as it is a Javascript object, which means there is no extra processing necessary.
If you do try to parse it, it should throw a syntax error
SyntaxError: Unexpected token o in JSON at position 1
you can just access it by
console.log(body.data.Object_Create.id)
I imagine you actually want to use that key however, so you could
const { Object_Create } = body.data
console.log(Object_Create.id)

Unable to send request to MongoDB nodeJS ADD through POSTMAN -

The question is pretty much self explanatory. I have created a backend for MongoDB using nodeJS. Here is the link to my entire backend project so you can replicate at your own end -->
https://onedrive.live.com/?cid=90b44987ff0f70ac&id=90B44987FF0F70AC%21157&authkey=!AOG8D2nRT--ga9I
Now, through Postman I am trying to send requests to the add operation to add new player items -
The steps I am following are -->
starting up mongo.exe and running use playerDB
starting up mongod.exe
opening command terminal on my backend project and running nodemon server
sending a POST request to Mongoose db through postman like
localhost:4000/playerDB/add
The error I am receiving is ->
400 Unable to add player object
My sample request body is like so:
{
"player_name":"Didier Drogba",
"player_description":"Brawny, cunning, determined and a heart for attempting the most impossible audacious! The one and only!",
"player_position":"Striker",
"player_age":"42",
"player_club":"Chelsea FC",
"player_":"Hall of Fame",
"player_isactive":"False",
"player_completed":"True"
}
What error did I make? What was the problem? Anythign wrong with the sequence of steps I followed?
Can you point out where I went wrong?
Thanks,
EDIT-->
The new error is:
<body>
<pre>SyntaxError: Unexpected token F in JSON at position 288
<br> at JSON.parse (<anonymous>)
<br> at parse (E:\REACT\MyProj\backend\node_modules\body-parser\lib\types\json.js:89:19)
<br> at E:\REACT\MyProj\backend\node_modules\body-parser\lib\read.js:121:18
<br> at invokeCallback (E:\REACT\MyProj\backend\node_modules\raw-body\index.js:224:16) .....
I have seen your code and there were a couple of changes that were needed.
1) In your player.model.js file, the module.exports should be at the end of the file.
This is because you are exporting the player schema even before it is declared and defined.
2) Two properties in the model: a) player_isactive and b) player_completed are defined as Boolean and you were passing a string from the postman. So you need to send the data as follows:
{
"player_name":"Didier Drogba",
"player_description":"Brawny, cunning, determined and a heart for attempting the most impossible audacious! The one and only!",
"player_position":"Striker",
"player_age":"42",
"player_club":"Chelsea FC",
"player_":"Hall of Fame",
"player_isactive": false,
"player_completed":true
}
Kindly make this changes and your problem is solved.

Azure Logic App: how to make a x-www-form-encoded?

I'm trying to make a request with Content-Type x-www-form-urlencoded that works perfectly in postman but does not work in Azure Logic App I receive a Bad Request response for missing parameters, like I'd not send enything.
I'm using the Http action.
The body value is param1=value1&param2=value2, but I tried other formats.
HTTP Method: POST
URI : https://xxx/oauth2/token
In Headers section, add the below content-type:
Content-Type: application/x-www-form-urlencoded
And in the Body, add:
grant_type=xxx&client_id=xxx&resource=xxx&client_secret=xxx
Try out the below solution . Its working for me .
concat(
'grant_type=',encodeUriComponent('authorization_code'),
'&client_id=',encodeUriComponent('xxx'),
'&client_secret=',encodeUriComponent('xxx'),
'&redirect_uri=',encodeUriComponent('xxx'),
'&scope=',encodeUriComponent('xxx'),
'&code=',encodeUriComponent(triggerOutputs()['relativePathParameters']['code'])).
Here code is dynamic parameter coming from the previous flow's query parameter.
NOTE : **Do not forget to specify in header as Content-Type ->>>> application/x-www-form-urlencoded**
Answering this one, as I needed to make a call like this myself, today.
As Assaf mentions above, the request indeed has to be urlEncoded and a lot of times you want to compose the actual message payload.
Also, make sure to add the Content-Type header in the HTTP action with value application/x-www-form-urlencoded
therefore, you can use the following code to combine variables that get urlEncoded:
concat('token=', **encodeUriComponent**(body('ApplicationToken')?['value']),'&user=', **encodeUriComponent**(body('UserToken')?['value']),'&title=Stock+Order+Status+Changed&message=to+do')
When using the concat function (in composing), the curly braces are not needed.
First of all the body needs to be:
{ param1=value1&param2=value2 }
(i.e. surround with {})
That said, value1 and value2 should be url encoded. If they are a simple string (e..g a_b) then this would be find as is but if it is for exmaple https://a.b it should be converted to https%3A%2F%2Fa.b
The easiest way I found to do this is to use https://www.urlencoder.org/ to convert it. convert each param separately and put the converted value instead of the original one.
Here is the screenshot from the solution that works for me, I hope it will be helpful. This is example with Microsoft Graph API but will work with any other scenario:

Resources