Parse error on Google GeoLocate API sample JSON - json

Has Google changed their GeoLocation api and not updated the documentation?
I have been following their example code verbatim off of the following page
https://developers.google.com/maps/documentation/geolocation/intro
I pasted the sample request into a file on my system called ex.json. I double checked that my Google Maps Geolocation API is set to on and executed the following curl command
curl -d ex.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=[My key, yes I pasted my actual key in]"
I received the following response
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
Which according to the documentation means that there is something wrong with the example json they provided. Just for completeness the sample json looks like
{
"homeMobileCountryCode": 310,
"homeMobileNetworkCode": 260,
"radioType": "gsm",
"carrier": "T-Mobile",
"cellTowers": [
{
"cellId": 39627456,
"locationAreaCode": 40495,
"mobileCountryCode": 310,
"mobileNetworkCode": 260,
"age": 0,
"signalStrength": -95
}
],
"wifiAccessPoints": [
{
"macAddress": "01:23:45:67:89:AB",
"signalStrength": 8,
"age": 0,
"signalToNoiseRatio": -65,
"channel": 8
},
{
"macAddress": "01:23:45:67:89:AC",
"signalStrength": 4,
"age": 0
}
]
}
JsonLint verified that it is proper Json, and the documentation says that all fields are optional. What am I missing, was some required field added after the documentation was written?

Curl needs "-X POST" as extra parameter. Works like this:
curl 'https://www.googleapis.com/geolocation/v1/geolocate?key=YOURKEY' -X POST -H "Content-Type: application/json" -d #yourjsonfile.json

Found the solution, it was a silly mistake :-
my file name was "sampledata.json" i changed this to "#sampledata.json"
I tried with three different curl commands. I got response.

Related

Google Safe Browsing invalid JSON payload error with curl using a file

When I run this command (on macOS zsh):
curl -X POST -v "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=[MY-API-KEY]" -H "Content-Type: application/json" -d #/path/to/my/test.json --http1.1
I get this error message:
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \" #/path/to/my/test.json\": Cannot bind query parameter. Field ' #/path/to/my/test' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \" #/path/to/my/test.json\": Cannot bind query parameter. Field ' #/path/to/my/test' could not be found in request message."
}
]
}
]
}
}
What's wrong with my command?
My test.json file contents is exactly as instructed here: https://developers.google.com/safe-browsing/v4/lookup-api

Questions on json and GCP

So I have this code to write out a json file to be connected to via endpoint. The file is pretty standard in that it has the location of how to connect to the endpoint as well as some data.
%%writefile default-pred.json
{ PROJECT_ID:"msds434-gcp",
REGION:"us-central1",
ENDPOINT_ID:"2857701089334001664",
INPUT_DATA_FILE:"INPUT-JSON",
"instances": [
{"age": 39,
"bill_amt_1": 47174,
"bill_amt_2": 47974,
"bill_amt_3": 48630,
"bill_amt_4": 50803,
"bill_amt_5": 30789,
"bill_amt_6": 15874,
"education_level": "1",
"limit_balance": 50000,
"marital_status": "2",
"pay_0": 0,
"pay_2":0,
"pay_3": 0,
"pay_4": 0,
"pay_5": "0",
"pay_6": "0",
"pay_amt_1": 1800,
"pay_amt_2": 2000,
"pay_amt_3": 3000,
"pay_amt_4": 2000,
"pay_amt_5": 2000,
"pay_amt_6": 2000,
"sex": "1"
}
]
}
Then I have this trying to connect to the file and then taking the information to connect to the end point in question. I know the information is right as it's the exact code from GCP.
!curl \
-X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-prediction-aiplatform.googleapis.com/v1alpha1/projects/$PROJECT_ID/locations/$REGION/endpoints/$ENDPOINT_ID:predict \
-d "#default-pred.json"
So from the information I have I would expect it to parse the information I have and connect to the endpoint, but obviously I have my file wrong somehow. Any idea what it is?
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"PROJECT_ID\": Cannot find field.\nInvalid JSON payload received. Unknown name \"REGION\": Cannot find field.\nInvalid JSON payload received. Unknown name \"ENDPOINT_ID\": Cannot find field.\nInvalid JSON payload received. Unknown name \"INPUT_DATA_FILE\": Cannot find field.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"PROJECT_ID\": Cannot find field."
},
{
"description": "Invalid JSON payload received. Unknown name \"REGION\": Cannot find field."
},
{
"description": "Invalid JSON payload received. Unknown name \"ENDPOINT_ID\": Cannot find field."
},
{
"description": "Invalid JSON payload received. Unknown name \"INPUT_DATA_FILE\": Cannot find field."
}
]
}
]
}
}
What am I missing here?
The data file should only include the data.
You've included PROJECT_ID, REGION, ENDPOINT and should not.
These need to be set in the (bash) environment before you issue the curl command:
PROJECT_ID="msds434-gcp"
REGION="us-central1"
ENDPOINT_ID="2857701089334001664"
curl \
--request POST \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header "Content-Type: application/json" \
https://us-central1-prediction-aiplatform.googleapis.com/v1alpha1/projects/$PROJECT_ID/locations/$REGION/endpoints/$ENDPOINT_ID:predict \
--data "#default-pred.json"
The file default-pred.json should probably (I can never find this service's methods in APIs Explorer!) just be:
{
instances": [
{ "age": 39,
"bill_amt_1": 47174,
"bill_amt_2": 47974,
"bill_amt_3": 48630,
"bill_amt_4": 50803,
"bill_amt_5": 30789,
"bill_amt_6": 15874,
"education_level": "1",
"limit_balance": 50000,
"marital_status": "2",
"pay_0": 0,
"pay_2":0,
"pay_3": 0,
"pay_4": 0,
"pay_5": "0",
"pay_6": "0",
"pay_amt_1": 1800,
"pay_amt_2": 2000,
"pay_amt_3": 3000,
"pay_amt_4": 2000,
"pay_amt_5": 2000,
"pay_amt_6": 2000,
"sex": "1"
}
]
}
See the documentation for the aiplatform predict method as this explains this.

Why Google Translate API doesn't accept API-Key in JSON request body?

I'm new to use Google Could Platform.
I tried Translation API's tutorial. By some reason I want to use API-Key for its authentication. but API doesn't accept key in JSON Request, though it accepts same key in HTTP query parameter.
Is this a restriction of Google Translation API? or do I have any mistake?
Following is what I tried:
Worked when API-key is passed as a query parameter
$ curl -H 'Content-Type: application/json' 'https://translation.googleapis.com/language/translate/v2?key=xxxxxxxxxx' --data-binary #test.json
with my test.json is:
{
"q": "The quick brown fox jumped over the lazy dog.",
"source": "en",
"target": "es",
"format": "text"
}
result was:
{
"data": {
"translations": [
{
"translatedText": "El rpido zorro marrn salt sobre el perro perezoso."
}
]
}
}
But for some security reason, I don't want to pass this API-key by query string.
Why I don't want to use query-string
An URI with sensitive parameter is not safe, for it is often logged or is shown in some situation, while HTTP Header or HTTP body aren't.
Of course using stronger authentication method (Service Account) is better for security, but API-key is also a good solution for some use cases like embedding in legacy-system, or so.
Didn't work when Api-key is passed in JSON request
I set "key" item in my request JSON, and it didn't work. It caused authorization error.
curl -H 'Content-Type: application/json' 'https://translation.googleapis.com/language/translate/v2' --data-binary #test.json
with test.json:
{
"key": "xxxxxxxxxx",
"q": "The quick brown fox jumped over the lazy dog.",
"source": "en",
"target": "es",
"format": "text"
}
result:
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"errors": [
{
"message": "The request is missing a valid API key.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
The only working example I could find was:
Append the key in the URL, e.g.:
https://translation.googleapis.com/language/translate/v2?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Do the rest in POST.

Github API v3 JSON passing

how can I avoid the parsing errors for
curl -H 'Accept: application/vnd.github.VERSION.raw' -XPUT -g 'https://api.github.com/repos/USER/l1/contents/PATH/FILENAME.json?ref=gh-pages&access_token=57eef6413b12cb439b837b8fc4751b3291650de1' -d '{
"message": "update from api",
"committer": {
"name": "USER",
"email": "USERe#MAIL.com"
},
"content": "[{"a": "aaa","b": "bbb"}]",
"sha": "c321fe9f6418053ecb87eb3cd2518a4xdfc83ebf"
}'
Answer:
{
"message": "**Problems parsing JSON**",
"documentation_url": "https://developer.github.com/v3/repos/contents/"
}
Instead of
"[{"a": "aaa","b": "bbb"}]"
I've tried
"[{\"a\": \"aaa",\"b\": \"bbb\"}]"
but then I get the following error:
{
"message": "**content is not valid Base64**",
"documentation_url": "https://developer.github.com/v3/repos/contents/"
}
Best,
If you open the URL to the documentation given in the error response, you'll see that the content needs to be Base64 encoded.

elasticsearch bulk insert JSON file

I have the following JSON file
I have used awk to get rid of empty spaces, trailing, next line
awk -v ORS= -v OFS= '{$1=$1}1' data.json
I have added a create request at the top of my data.json followed by \n and the rest of my data.
{"create": {"_index":"socteam", "_type":"products"}}
When I issue bulk submit request, I get the following error
CURL -XPUT http://localhost:9200/_bulk
{
"took": 1,
"errors": true,
"items": [
{
"create": {
"_index": "socteam",
"_type": "products",
"_id": "AVQuGPff-1Y7OIPIJaLX",
"status": 400,
"error": {
"type": "mapper_parsing_exception",
"reason": "failed to parse",
"caused_by": {
"type": "not_x_content_exception",
"reason": "Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"
}
}
}
}
]
Any idea on what this error mean? I haven't created any mapping, I'm using vanilla elasticsearch
Accordingly to this doc, you have to specify index and type in URL:
curl -XPUT 'localhost:9200/socteam/products/_bulk?pretty' --data-binary "#data.json"
It works for PUT and POST methods.
And your data.json file should have structure like:
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
Maybe there present another method to import data, but i know just this... Hope it'll help...