parse_exception - request body is required - elasticsearch

I'm trying to insert a JSON data file in my elasticsearch instance.
curl -s -H “Content-Type: application/x-ndjson” -XPOST localhost:9200/_bulk —-data-binary “#restaurants.json”; echo
However, after executing this command I get an error that says
{"error":{"root_cause":[{"type":"parse_exception","reason":"request body is required"}],"type":"parse_exception","reason":"request body is required"},"status":400}
The JSON file basically has an array of the below object.
Only thing is that I've put just one object here to save space. However, there are more than one objects present.
Structure is like given below;
[
{
"address": {
"building": "351",
"coord": [
-73.98513559999999,
40.7676919
],
"street": "West 57 Street",
"zipcode": "10019"
},
"borough": "Manhattan",
"cuisine": "Irish",
"name": "Dj Reynolds Pub And Restaurant",
"grades": [
{
"date": {
"$date": "2014-09-06T00:00:00.000Z"
},
"grade": "A",
"score": 2
},
{
"date": {
"$date": "2013-07-22T00:00:00.000Z"
},
"grade": "A",
"score": 11
},
{
"date": {
"$date": "2012-07-31T00:00:00.000Z"
},
"grade": "A",
"score": 12
},
{
"date": {
"$date": "2011-12-29T00:00:00.000Z"
},
"grade": "A",
"score": 12
}
],
"id": "30191841"
}
]

The bulk API requires one document per line, which means you can't have newlines in your documents. Try stripping all the white spaces from the JSON you're submitting. You are also just submit a stream of documents, and not a JSON array of objects. See Bulk API documentation here: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

I had this same issue in Windows and am posting this here for anyone that comes across the same question with this error referenced.
{"error":{"root_cause":[{"type":"parse_exception","reason":"request body is required"}],"type":"parse_exception","reason":"request body is required"},"status":400}
I found at least two causes:
cURL doesn't know where the file you are trying to load is:
In this case, make sure to run cURL from the directory where the file is located. Try executing the following command and making sure you see the file.
more file.json
Windows command line DOES NOT support single quotes within the cURL command line:
BAD
curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/_bulk' --data-binary '#file.json'
GOOD
curl -H "Content-Type: application/x-ndjson" -XPOST "localhost:9200/_bulk" --data-binary "#file.json"

I solved it by wrapping the url with quotation mark.
curl -s -H “Content-Type: application/x-ndjson” -XPOST "localhost:9200/_bulk —-data-binary “#restaurants.json”;

Related

Postman gives me the following error: "error": "no handler found for uri [/megacorp/employee/1] and method [PUT]"

I am just starting with Elasticsearch and I have started with adding an index, which works and I can get information about it:
GET http://localhost:9200/megacorp
"megacorp": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "megacorp",
"creation_date": "1657286196414",
"number_of_replicas": "1",
"uuid": "HbsAAv-mRziSUKGiXPMyPA",
"version": {
"created": "8030299"
The problem comes when I try to add a document, I get the following error:
PUT http://localhost:9200/megacorp/empoyee/1
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": ["sports", "music"]
"error": "no handler found for uri [/megacorp/empoyee/1] and method [PUT]"
I think I've done everything right, but it still does not work.
Problem here, that you are using wrong URL in your request.
According to documentation you must use use following paths for document index:
Request
PUT /<target>/_doc/<_id>
POST /<target>/_doc/
PUT /<target>/_create/<_id>
POST /<target>/_create/<_id>
So you are missing _doc or _create part.
UPDATE:
cURL Example
curl -X PUT --location "http://localhost:9200/megacorp/1" \
-H "Content-Type: application/json" \
-d "{
\"first_name\": \"John\",
\"last_name\": \"Smith\",
\"age\": 25,
\"about\": \"I love to go rock climbing\",
\"interests\": [\"sports\", \"music\"]
}"
From elasticsearch 8.x mapping types are removed. You need to use this for indexing the documents
http://localhost:9200/megacorp/_doc/1

How to write json output to a CSV file using jq in a bash script?

I have a JSON response from a API call as below and would like to write this to CSV file through a shell script.
JSON response:
{
"maxResults": 50,
"startAt": 0,
"isLast": true,
"values": [
{
"id": 1986,
"startDate": "2020-01-27T20:15:30.094Z",
"endDate": "2020-02-24T20:15:00.000Z"
},
{
"id": 1987,
"startDate": "2020-02-24T20:48:40.618Z",
"endDate": "2020-03-15T20:48:00.000Z"
},
{
"id": 1988,
"startDate": "2020-03-16T17:46:16.846Z",
"endDate": "2020-04-24T17:46:00.000Z"
},
{
"id": 1989,
"startDate": "2020-04-20T17:59:30.920Z",
"endDate": "2020-06-10T17:59:00.000Z"
}
]
}
the CSV file should look something like below
1986,2020-01-27T20:15:30:094Z,2020-02-24T20:15:00:000Z
1987,2020-02-24T20:48:40.618Z,2020-03-15T20:48:00.000Z
1988,2020-03-16T17:46:16.846Z,2020-04-24T17:46:00.000Z
1989,2020-04-20T17:59:30.920Z,2020-06-10T17:59:00.000Z
I have tried using jq but unable to figure out the correct way to write to csv
curl -X GET \
--header 'Authorization: Basic <token>' \
--header 'Content-Type: application/json'\
"$url" | jq -r '[.values[].id, .values[].startDate, .values[].endDate] | #csv ' >> dates.csv
Any inputs would be helpful.
this may depend on the api endpoint, but you could just request for CSV when calling your API. curl -X GET -H "Content-Type: text/csv" 'https://some.api.endpoint.com/api/request.csv' so you get the CSV response instead of having to convert it yourself.

Issuecommand RESET_PASSWORD on android management api doesn't change lock code of the device et LOCK_NOW flag doesn't lock the device either

I'm trying to reset the lock code of my device through android management API but it seems that RESET_PASSWORD doesn't do anything. The endpoint is https://androidmanagement.googleapis.com/v1/enterprises/entrepriseID/devices/deviceID:issueCommand and my payload looks like this
{
"type": "RESET_PASSWORD" ,
"duration": "600s",
"newPassword":"1234",
"resetPasswordFlags":["LOCK_NOW"]
}
Does anyone find any solution ?
I have the same problem with this command :
I find the command in the documentation.
When i execute this CURL, it answer me 200 OK.
curl --location --request POST 'https://androidmanagement.googleapis.com/v1/enterprises/<enterprise>/devices/<device_id>:issueCommand' \
--header 'content-type: application/json' \
--header 'Authorization: Bearer <TOKEN>' \
--data-raw '{
"type": "RESET_PASSWORD",
"duration": "600s"
}'
{
"name": "enterprises/<enterprise>/devices/<device_id>/operations/<operation_id>",
"metadata": {
"#type": "type.googleapis.com/google.android.devicemanagement.v1.Command",
"type": "RESET_PASSWORD",
"createTime": "2022-05-03T09:34:15.913Z",
"duration": "600s",
"userName": "enterprises/<enterprise>/users/<user_id>"
}
}
But, when i try to get the health of the operation, it answer to me an error:
curl --location --request GET 'https://androidmanagement.googleapis.com/v1/enterprises/<enterprise>/devices/<device_id>/operations/<operation_id>' \
--header 'content-type: application/json' \
--header 'Authorization: Bearer <TOKEN>'
Answer (200 OK):
{
"name": "enterprises/<enterprise>/devices/<device_id>/operations/<operation_id>",
"metadata": {
"#type": "type.googleapis.com/google.android.devicemanagement.v1.Command",
"type": "RESET_PASSWORD",
"createTime": "2022-05-03T09:34:15.913Z",
"duration": "600s",
"errorCode": "INVALID_VALUE",
"userName": "enterprises/<enterprise>/users/<user_id>"
},
"done": true,
"error": {
"code": 3
}
}
i don't know which invalid value i put into the params..
More, the LOCK or REBOOT command work correctly without any error for the same device.
Sincerely.
Adrien.
I tried to recreate the scenario using the same settings that you are using and I was able to reset my password and change it to the new password.
device_name = enterprise_name + '/devices/deviceId'
device_json = '''
{
"duration": "600s",
"type": "RESET_PASSWORD",
"newPassword": "12345",
"resetPasswordFlags": [
"LOCK_NOW"
]
}
'''
androidmanagement.enterprises().devices().issueCommand(
name=device_name,
body=json.loads(device_json)
).execute()
This API seems to be working properly in my end. For this API to work properly, please make sure the newPassword value meets any passwordRequirements you have set in the policy. Also, you can check the device if it receives the command as it should lock its screen automatically upon receiving the command.
You can also try using other commands to ensure that the issue is not in your device or connection.

curl request says to address needs to be an array when it already is?

I'm trying to send an email out using Mandrill by adapting the curl command from https://mailchimp.com/developer/transactional/api/messages/send-new-message/ and using the API key from https://us1.admin.mailchimp.com/account/api/ and I'm getting an error that doesn't make a lot of sense to me. Here's the command I'm running:
curl -X POST \
https://mandrillapp.com/api/1.0/messages/send \
-d '{"key":"...","message":{"html":"hello, world!","subject":"hello, world!","from_email":"neubert#neubert.com","to":["neubert#neubert.com"]}}'
Here's the response:
{"status":"error","code":-2,"name":"ValidationError","message":"Validation error: {\"message\":{\"to\":[\"Please enter an array\"]}}"}
Here it is decoded:
{
"status": "error",
"code": -2,
"name": "ValidationError",
"message": "Validation error: {\"message\":{\"to\":[\"Please enter an array\"]}}"
}
I don't understand. "to" is an array...
Any ideas?
Thanks!
I also got the same error at first then going through the documentation keenly, I noticed that the to field in message is NOT a list of email addresses. I know it's intuitive that way!!
It's a list of object's with 3 fields: email, name and type.
An ideal JSON Body will be as follows:
{
"key": "<YOUR_MANDRILL_API_KEY>", # Caution! It is not a mailchimp API key
"message": {
"html": "testing html part",
"text": "testing text part",
"subject": "testing subject",
"from_email": "<FROM_EMAIL_ADDRESS>",
"from_name": "<FROM_NAME>",
"to": [
# Recipient 1
{
"email": "<Recipient1_EMAIL_ADDRESS>",
"name": "<Recipient1_NAME>"
},
# Recipient 2
{
"email": "<Recipient2_EMAIL_ADDRESS>",
"name": "<Recipient2_NAME>"
}
]
}
}

Pullrequest to Bitbucket

I am looking to send pull request to multiple reviewers in bitbucket. Currently I have json and curl request as below
{
"title": "PR-Test",
"source": {
"branch": {
"name": "master"
}
},
"destination": {
"branch": {
"name": "prd"
}
},
"reviewers": [
{
"uuid": "{d543251-6455-4113-b4e4-2fbb1tb260}"
}
],
"close_source_branch": true
}
curl -u "user:pass" -H "Content-Type: application/json" https://api.bitbucket.org/2.0/repositories/companyname/my-repo/pullrequests -X POST --data #my-pr.json
The above curl command works. I need the json syntax to pass either multiple usernames or multiple UUID in the reviewers list.
https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pullrequests
The documentation you linked to seems to indicate that something like this should work:
"reviewers": [
{
"uuid": "{504c3b62-8120-4f0c-a7bc-87800b9d6f70}"
},
{
"uuid": "{bafabef7-b740-4ee0-9767-658b3253ecc0}"
}
]

Resources