Rails JSON unexpected token error - ruby-on-rails-3

I'm perplexed by the issues I'm seeing with my JSON. I am using my controller to inject some values, read from a file from my public folder, into a fxn call.
app/views/comparison/graphs.html.haml:
- tmp = File.readlines(txt)
:javascript
generate_plot(#{tmp.first})
which generates this in my html:
<script>
//<![CDATA[
generate_plot([[[22.0,307.0,491.0,35.0,580.0,40.0,387.0,28.0,34.0,519.0,4.0,59.0,33.0,448.0,24.0,341.0],["2011-04-27T05:32:00-06:00","2011-04-27T05:32:00-06:00","2011-05-20T02:49:00-06:00","2011-05-20T02:49:00-06:00","2010-10-12T23:27:00-06:00","2010-10-12T23:27:00-06:00","2010-11-21T19:20:00-07:00","2010-11-21T19:20:00-07:00","2011-11-03T08:59:00-06:00","2011-11-03T08:59:00-06:00","2011-02-22T03:24:00-07:00","2011-02-22T03:24:00-07:00","2010-08-17T20:26:00-06:00","2010-08-17T20:26:00-06:00","2011-11-09T06:15:00-07:00","2011-11-09T06:15:00-07:00"]],[[24.0,463.0,566.0,31.0,864.0,57.0,28.0,596.0,717.0,47.0,31.0,28.0,596.0,35.0,597.0,622.0,463.0,24.0,463.0,24.0,719.0,51.0,556.0,30.0,51.0,891.0,30.0,556.0,494.0,38.0,23.0,443.0,581.0,28.0,581.0,28.0,556.0,30.0,622.0,31.0],["2010-10-15T10:04:00-06:00","2010-10-15T10:04:00-06:00","2010-09-20T04:24:00-06:00","2010-09-20T04:24:00-06:00","2010-01-08T02:10:00-07:00","2010-01-08T02:10:00-07:00","2010-09-02T20:40:00-06:00","2010-09-02T20:40:00-06:00","2010-07-08T13:11:00-06:00","2010-07-08T13:11:00-06:00","2010-02-22T23:10:00-07:00","2010-09-15T04:13:00-06:00","2010-09-15T04:13:00-06:00","2010-05-08T19:01:00-06:00","2010-05-08T19:01:00-06:00","2010-02-22T23:10:00-07:00","2010-01-10T18:59:00-07:00","2010-01-10T18:59:00-07:00","2010-06-23T14:10:00-06:00","2010-06-23T14:10:00-06:00","2010-04-04T21:57:00-06:00","2010-04-04T21:57:00-06:00","2010-01-09T11:01:00-07:00","2010-01-09T11:01:00-07:00","2010-04-30T02:10:00-06:00","2010-04-30T02:10:00-06:00","2010-10-08T13:59:00-06:00","2010-10-08T13:59:00-06:00","2010-03-03T13:12:00-07:00","2010-03-03T13:12:00-07:00","2010-06-29T15:06:00-06:00","2010-06-29T15:06:00-06:00","2010-02-14T05:47:00-07:00","2010-02-14T05:47:00-07:00","2010-06-18T05:23:00-06:00","2010-06-18T05:23:00-06:00","2010-05-12T20:05:00-06:00","2010-05-12T20:05:00-06:00","2010-03-04T19:18:00-07:00","2010-03-04T19:18:00-07:00"]],2])
//]]>
</script>
I've checked and rechecked my input, but I get this error:
Unexpected token error ,
My Json is valid, when I simply delete the parenthesis and the function call:
http://jsfiddle.net/VVL8C/
I'm not sure what else could be going wrong.
Thanks

Here is a valid JSON example
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
http://www.json.org/js.html
At the end instead of ,2]) try
,[[2],[""]] )

Related

Saving a Postman collection variable from he response body

Trying to figure out why I cannot get this to work? Also, console does not give much of a result.
Scenario:
Making the POST request to get the response with TOKEN
Save the response token to collection variable (as the collection file will be used for importing to another testing solution in the cloud)
Using that collection variable to log out from the session
So, I need to be able to store this as a collection variable and use that token when logging out from the session/DELETE the API admin session.
Error in the console:
There was an error in evaluating the test script: JSONError: Unexpected token 'o' at 1:2 [object Object] ^
Tests:
var response = pm.response.json()
var jsonData = JSON.parse(response)
pm.collectionVariables.set("token", jsonData.response.token);
Response body:
{
"response": {
"token": "***"
},
"messages": [
{
"code": "0",
"text": "OK"
}
]
}
Thank you very much for any advice!
JSON.parse(responseBody) always gets a json representation of the response.
A complete fail safe approach would be:
pm.test("response is ok", ()=>{
pm.response.to.have.status(200)
})
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.token);
Here's the approach i used.
Send the request
Check if there response is 200
If it's true set the token
pm.test("response is ok", ()=>{
if( pm.response.to.have.status(200)){
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.token);
}
})
You need to change your status code depending on the condition.
Hope it helps

expo camera react native

I am using the expo camera feature to submit scanned images. I have used TakePicutreAsync and have a base64 image. The scan is working fine. But at the time of submitting images, there following is the server error when I try to send a HTTP POST request in try catch block,
Cannot read properties of undefined (reading '1')
and on front end it errors the partial request body (everything minus the features, see below) and that is the error object from which I can make no sense of the error.
I am using formData to form my request as below:
const body = {
frontImg: frontImageBase64,
backImg: backImageBase64
}
var obj1 = {"content": JSON.stringify(body)};
var obj2 = [{"type": "LABEL_DETECTION"}];
formData['image'] = obj1;
formData['features'] = obj2;
Here is the complete request formed ( part of which is spit out as error body from try(POST Request) catch block
FormData {
"_parts": Array [],
"features": Array [
Object {
"type": "LABEL_DETECTION",
},
],
"image": Object {
"content": "{\"frontImage\":\"/9j/4AAQSkZJRgABAQAAAQABAAD/4g...
}
}
What could be going wrong in submitting images or how do I debug with the error message being same as the request body with just image property and not features and nothing else?
Below is the error object body for reference.
error Object {
"config": Object {
"adapter": [Function xhrAdapter],
"data": "{\"_parts\":[],\"image\":{\"content\":\"{\\\"frontImage\\\":\\\"/9j/4AAQSkZAAAAA...

React Native fetch() function gives SyntaxError: Unexpected end of JSON input

Anybody please tell me what am i doing wrong here .
P.S. When call the api using POSTMAN it goes through with same data given as RAW/JSON input
Here is the code:`
fetch(url,{
headers:{
"ContentType": "text/json"
},
method:"POST",
body:{"apiKey": "12345", "clientId": "254455",
"callerId": "02656708557","CallingAgentNumber": "09547142951",
"CalledPartyNumber": "0891879567", "callType":"2"}
}).then(res=>res.json()).then(result=>console.log(result)).catch(err=>console.log(err));
`

Karate: JSON is converted to string ([object Object]) when matching

I'm trying to match the response of an API with a schema. I have the following setup.
Here is the feature file:
Feature: As a user, I would like to test APIs of member modules.
Background:
* def DIR_PATH = 'data/users/'
* def signup_request_helpers = read(<path_to_js_file>)
* def signup_response_helpers = read(<path_to_js_file>)
* def request_data = signup_request_data()
Scenario: Test signup of user
Given url my_url
And request request_data
When method POST
Then status 200
Then match response == signup_response_schema
Here is the JS file that holds the response schema
var signup_response_schema = {
"success": true,
"message": "Successfully signed up.",
"data": {
"user": user_schema,
"confirmation_url": "#string",
"token": "#string",
"role": "#array"
}
};
The problem is that karate seems to be converting the response schema into a string. This is the error message that I get:
actual: {<response>}, expected: [object Object]
Please note that I can match individual string fields fine. The problem only occurs when I try to match objects. Also, I have tried printing the schema and the variable seems to be holding the correct value which is the schema object.
I've tried the following:
Convert the schema into a string and then a JSON using the library's directives. This doesn't verify the schema
Convert directly to json which still renders the schema as [object Object]
NOTE: I'm running Karate on docker if that makes any difference. Following is the command that I use in my docker-compose file:
java -jar -Dkarate.config.dir=/app /app/karate-0.9.1.jar -T 5 path_to_file
JS in Karate is not that seamless. This will work:
var temp = {
"success": true,
"message": "Successfully signed up.",
"data": {
"user": user_schema,
"confirmation_url": "#string",
"token": "#string",
"role": "#array"
}
};
karate.set('signup_response_schema', temp);
Normally in Karate *.js files start with function and contain a single function block. And the result of reading (evaluating) that block is assigned to a Karate variable using def. But the recommended option for you is this, where you just have a JSON in the file, and the extension matters:
Then match response == read('signup_response_schema.json')
Note that read() will also evaluate embedded expressions, which can be useful. Although the JSON is not expected to be strictly well-formed, it is recommended.
The issue here wasn't of JS evaluation. The variable signup_response_schema holds the value that I assigned to it in the JS file.
The problem was with the actual matching of the schema because one of the keys was missing in the actual response. Karate usually throws specific error messages indicating which keys were missing but for some reason, this isn't the case here.

POST command to WebCeo API

Trying to connect to this WebCEO API.
function getProjects() {
var payload = {
"key": "CUSTOMER_KEY",
"method": "get_projects"
};
payload = JSON.stringify(payload);
var url = "https://online.webceo.com/api/";
var options = {
"method": 'POST',
"contentType" : "application/json",
"payload": payload
};
var response = UrlFetchApp.fetch(url, options);
}
Receiving "Request failed for https://online.webceo.com/api/ returned code 404".
Any hint on what else I need to include / change?
Body must contain the following:
json={"key": "YOUR_API_KEY", "method": "get_projects"}
Well, https://online.webceo.com/api/ does return a 404 when you just try to access it. Did you manage to get that page to not return a 404 error from another client?
Doing so will probably tell you what you're missing here.
However, I'd suspect their API might be having issues.
That's true, you don't make a GET request. You have to send parameters in the body of a POST request. Below is an example in CURL for a situation when you need to get the list of projects:
curl -X POST -d 'json={"key": "YOUR_API_KEY", "method": "get_projects" }' https://online.webceo.com/api/