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.
Related
While following the Introduction to APIs in Google lab, I'm facing an issue while creating a storage bucket. I followed the instructions but still the following is shown:
Run the following command to create a Cloud Storage bucket:
curl -X POST --data-binary #values.json
-H "Authorization: Bearer $OAUTH2_TOKEN"
-H "Content-Type: application/json"
"https://www.googleapis.com/storage/v1/b?project=$PROJECT_ID"
Error:
{
"error": {
"code": 400,
"message": "Invalid bucket name: '\u003cqwiklabs-gcp-00-eb5dde0a1183-bucket'",
"errors": [
{
"message": "Invalid bucket name: '\u003cqwiklabs-gcp-00-eb5dde0a1183-bucket'",
"domain": "global",
"reason": "invalid"
}
]
}
}
1.Edit the values.json file
nano values.json
{ "name": "quicklab-test-your-bucket",
"location": "us",
"storageClass": "multi_regional"
}
2.Get the token:
gcloud auth print-access-token
3.Set the variables:
export OAUTH2_TOKEN=your-token
export PROJECT_ID=your-project-id
4.Create the bucket:
curl -X POST --data-binary #values.json \
-H "Authorization: Bearer $OAUTH2_TOKEN" \
-H "Content-Type: application/json" \
"https://www.googleapis.com/storage/v1/b?project=$PROJECT_ID"
5.You should receive similar output:
{
"kind": "storage#bucket",
"id": "qwiklabs-test-bucket",
"selfLink": "https://www.googleapis.com/storage/v1/b/sean123456789",
"projectNumber": "218136653205",
"name": "sean123456789",
"timeCreated": "2018-10-19T21:04:03.604Z",
"updated": "2018-10-19T21:04:03.604Z",
"metageneration": "1",
"location": "US",
"storageClass": "MULTI_REGIONAL",
"etag": "CAE="
}
I have this json object but i don't know if -d is reference to body or is another thing.
curl -X POST \'https://api.mercadopago.com/checkout /preferences?access_token=ACCESS_TOKEN_ENV' \
-H 'content-type:application/json' \
-d '{
"items": [
{
"title": "Dummy Item",
"description": "Multicolor Item",
"quantity": 1,
"currency_id": "ARS",
"unit_price": 10.0
}]
}'
The -d parameter to curlmeans data. That is the body of the http request.
https://curl.haxx.se/docs/manpage.html#-d
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.
I'm getting this error:
error": { "code": 400, "message": "Invalid value at 'requests[0].image.content' (TYPE_BYTES), \"000002.jpg\"", "status": "INVALID_ARGUMENT", "details": [ { "#type": "type.googleapis.com/google.rpc.BadRequest"
And it may have something to do with the encoding of the images, not sure. I am sending Google a bunch of .jpgs.
Note that this is similar to this question, but that one doesn't help a whole lot - there's no answer.
Here's my JSON:
for f in $FILES
do
echo "Original file name is $f"
response=$(curl -v -H "Accept: application/json" -H "Content-type:
application/json" -X POST -d '{"requests":[
{
"image":{
"content":"'"$f"'"
},
"features":[
{
"type":"LABEL_DETECTION",
"maxResults":3
}
]
}]}' $baseURL)
echo $response
done
This is the first image I send it.
Any thoughts on what's causing this?
You need to send the base64 encoded representation of the image, not the filename.
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.