0% found this document useful (0 votes)
62 views

Single Quote API

The document discusses escaping single quotes in API queries. It provides an example query that causes a syntax error due to an unescaped single quote. To fix this, the single quote can be escaped with a backslash or replaced with its Unicode encoding. The document demonstrates how to normalize the query string to escape the single quote, and shows the successful API response when running the normalized query.

Uploaded by

Zain Ihsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Single Quote API

The document discusses escaping single quotes in API queries. It provides an example query that causes a syntax error due to an unescaped single quote. To fix this, the single quote can be escaped with a backslash or replaced with its Unicode encoding. The document demonstrates how to normalize the query string to escape the single quote, and shows the successful API response when running the normalized query.

Uploaded by

Zain Ihsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Single Quotes in queries & API Calls

Escaping apostrophe (single quote) character in API calls

let’s take a look at the following sample query :

"INSERT{'name' : 'Julie', 'company' : 'Julie's pizza', '_key' : 'Julie'} INTO testcollection"

There’s a single ‘ embedded into single quotes in the values of the company key

The cURL API call generated for the above query is as follows:

curl -X 'POST'
'https://api-gdn.paas.macrometa.io/_fabric/_system/_api/query'
-H 'accept: application/json'
-H 'Content-Type: application/json'
-H 'Authorization: bearer xxxx'
-d '{
"query": "INSERT{'\''name'\'' : '\''Julie'\'', '\''company'\'' : '\''Julie'\''s pizza'\'', '\''_key'\'' : '\''Julie'\''}
INTO testcollection"
}'

When executed, the following response is recorded by the command line:

{
"code": 400,
"error": true,
"errorMessage": "syntax error, unexpected identifier, expecting } near 's pizza', '_key' : 'Julie'}
INTO...' at position 1:45",
"errorNum": 1501
}

The error message explains the error encountered by the compiler while trying to execute the
query.
As iterated before, the error is caused by the single quotation mark embedded into single
quotation marks.
Therefore, it is recommended to use normalization , let’s take a look at the corrected query :

INSERT{'name' : 'Julie', 'company' : \"Julie's Pizza\", '_key' : 'Julie'} INTO testcollection


-----------------------------------------------------------------------------------------------------------------------------
How to normalize?

Assuming the value to enter is as follows : “I'm planning to write a book someday.”
Normalized string would be : \"I'm planning to write a book someday.\"

-----------------------------------------------------------------------------------------------------------------------------

Now let’s execute the new normalized query, which will require the following API call:

curl -X 'POST' \
'https://api-gdn.paas.macrometa.io/_fabric/_system/_api/query'
-H 'accept: application/json'
-H 'Content-Type: application/json'
-H 'Authorization: bearer xxxx' \
-d '{
"query": " INSERT{'\''name'\'' : '\''Julie'\'', '\''company'\'' : \"Julie'\''s Pizza\", '\''_key'\'' : '\''Julie'\''}
INTO testcollection "
}'

Which will return the following response:


{
"error": false,
"code": 200,
"parsed": true,
"collections": [
"testcollection"
],
"bindVars": [],
"ast": [
{
"type": "root",
"subNodes": [
{
"type": "insert",
"subNodes": [
{
"type": "no-op"
},
{
"type": "collection",
"name": "testcollection"
},
{
"type": "object",
"subNodes": [
{
"type": "object element",
"name": "name",
"subNodes": [
{
"type": "value",
"value": "Julie"
}
]
},
{
"type": "object element",
"name": "company",
"subNodes": [
{
"type": "value",
"value": "Julie's Pizza"
}
]
},
{
"type": "object element",
"name": "_key",
"subNodes": [
{
"type": "value",
"value": "Julie"
}
]
}
]
},
{
"type": "variable",
"name": "$NEW",
"id": 0
}
]
}
]
}
]
}

"value": "Julie's Pizza" in the response body means the apostrophe was added correctly.

Alternatively, you can also replace a single quotation mark with <\u0027> also known as “by
unicode encoded”:

Error text: “Julie’s pizza”


Correction: “Julie\u0027s pizza”

Now replacing the correction back into the query:

INSERT{'name' : 'Julie', 'company' : \"Julie\u0027s Pizza\", '_key' : 'Julie'} INTO testcollection

API call:

curl -X 'POST' 'https://api-gdn.paas.macrometa.io/_fabric/_system/_api/query' -H 'accept:


application/json' -H 'Content-Type: application/json' -H 'Authorization: bearer xxxx' -d '{
"query": " INSERT{'\''name'\'' : '\''Julie'\'', '\''company'\'' : \"Julie\u0027s Pizza\", '\''_key'\'' :
'\''Julie'\''} INTO testcollection "}'

Which gives the response:


{
"error": false,
"code": 200,
"parsed": true,
"collections": [
"testcollection"
],
"bindVars": [],
"ast": [
{
"type": "root",
"subNodes": [
{
"type": "insert",
"subNodes": [
{
"type": "no-op"
},
{
"type": "collection",
"name": "testcollection"
},
{
"type": "object",
"subNodes": [
{
"type": "object element",
"name": "name",
"subNodes": [
{
"type": "value",
"value": "Julie"
}
]
},
{
"type": "object element",
"name": "company",
"subNodes": [
{
"type": "value",
"value": "Julie's Pizza"
}
]
},
{
"type": "object element",
"name": "_key",
"subNodes": [
{
"type": "value",
"value": "Julie"
}
]
}
]
},
{
"type": "variable",
"name": "$NEW",
"id": 0
}
]
}
]
}
]
}

The “value” recorded in the response body is "Julie's Pizza" which is the correct
representation.

You might also like