Content-type header not supported - elasticsearch

I am following this link for elasticsearch.
https://www.elastic.co/blog/a-practical-introduction-to-elasticsearch
I am trying following curl to post the json data.
curl -XPOST "http://localhost:9200/shakespeare/_bulk?pretty" --data-binary #D:\data\shakespeare.json
But I am getting error like below:
{
"error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
"status" : 406
}

You need to set the content type in the header to application/json:
curl -XPOST -H "Content-Type: application/json" "http://localhost:9200/shakespeare/_bulk?pretty" --data-binary #D:\data\shakespeare.json

I received the same error after updating from an older version to 6.x.x. I'm not using curl statements directly, I'm using python's elasticsearch plugin.
In my case, I needed to install the newer version of the plugin that corresponded to the updated elasticsearch server.
pip install elasticsearch==6.3.1
Make sure you run it in the same Python Environment that your code is executing in.
Hope this saves someone some headache.

I found a solution you can set a default header that will overwrite previous header:
RestClient restClient = RestClient
.builder(new HttpHost(url, port, scheme))
.setDefaultHeaders(new Header[]{
new BasicHeader("Content-type", "application/json")
})
.build();

Related

Is there a way to set the user in org.springframework.web.client.RestTemplate

I'm trying to migrate a curl command with a username and password to a rest API consumer code of org.springframework.web.client.RestTemplate
curl -i -k --location -u username:userpass \
--request GET http://myserver:80/rest/api/2/project --header 'Accept: application/json' \
--header 'Content-Type: application/json' --data-raw ''
But such parameter username:password is not supported in RestTemplate (correct me if I'm wrong)
What other options can do this?
I'm using spring boot 2.4.3
If you use -u or --user, Curl will Encode the credentials into Base64 and produce a header like this: -H Authorization: Basic <Base64EncodedCredentials>
There is a way to build a RestTemplate with what you want to achieve. To do that just configure a singleton restTemplate bean in your configuration class.
Until version 2.1.0 it was available the basicAuthorization()
previous spring boot versions used basicAuthorization()
#Bean
RestTemplate rest(RestTemplateBuilder restTemplateBuilder) {
restTemplateBuilder.basicAuthorization("username", "userpass").build();
}
From 2.1.0 and forward the basicAuthorization() has been deprecated and in later versions removed. You can use basicAuthentication() instead
newer versions have only basicAuthentication()
#Bean
RestTemplate rest(RestTemplateBuilder restTemplateBuilder) {
restTemplateBuilder.basicAuthentication("username", "userpass").build();
}

Spring boot Cookie versus Authorization in header field

I have a very minimal spring-boot app with dependency spring-boot-starter-web and spring-boot-starter-security. I just learned spring boot yesterday. I noticed that sending a POST request like below even if provided with the wrong basic auth password the request succeeds. Because, of the presence of a cookie in a previous successful request.
curl --location --request POST 'http://localhost:8080/create' --header 'Content-Type: application/json' --header 'Cookie: JSESSIONID=EA39A09B47575D192845148AFFCAD85B' --data-raw '{
"surname":"Murdock",
"givenname":"John",
"placeofbirth":"Slovenia",
"pin":"1234"
}'
Is this the expected behavior? And how do I make spring-boot to always check the provided basic auth password?

Xcode Server Bots API: Updating a bot with PATCH request

I would like to edit my Xcode bot through the Xcode Server API by sending the blueprint through PATCH.
However, when I send my PATCH request, Xcode Server replies back an unchanged json of my old blueprint.
My request is curl -X PATCH -H "Content-Type: application/json" -d "{\"my\": \"json\"}" https://<username>:<password>#<my_domain>:20343/api/bots/<bot_id>
What am I missing?
There are two missing parameters that will cause the following problems:
Missing xcsclientversion: server will return 400 Bad Request.
Missing overwriteBlueprint=true: server will not change the blueprint.
Your final request should look like the following:
curl -X PATCH -H "Content-Type: application/json" -H "x-xcsclientversion: 18" -d "{\"json goes\": \"here\"}" https://<username>:<password>#<domain>:20343/api/bots/<_id>?overwriteBlueprint=true
Source: radar and Developer Relations (thanks!)

Making a POST request with Chef

I need to post a .json file to a server with a rest API with a Chef recipe, following Chef's documentation I came up with this code:
http_request '/tmp/bpp.json' do
url 'http://localhost:8080/api/v1/blueprints/bpp'
headers ({
'AUTHORIZATION' => "Basic #{Base64.encode64(user)}",
'CONTENT-TYPE' => 'aplication/json'
})
action :post
end
For authorization token, user is a variable that contains 'user:password'
When I run this chef recipe I obtain the following response:
Error executing action `post` on resource 'http_request[POST /tmp/bpp.json]'
Net::HTTPServerException
------------------------
400 "Bad Request"
Prior to this I was just executing a curl call and it was working fine, but I need to change to the http_request resource. This was the old (working) curl request:
curl --user user:password -H 'X-Requested-By:My-cookbook' --data #/tmp/bpp.json localhost:8080/api/v1/blueprints/bpp
I am not very used with REST apis and seems like an uncharted territory to me.
You forget about message. Using file name as resource name won't send this file as data. Try adding:
...
message lazy { IO.read('/tmp/bpp.json') }
...
In your case only the resource name - /tmp/bpp.json, will be sent. Not a file content. As stated in linked doc:
The message that is sent by the HTTP request. Default value: the name of the resource block See “Syntax” section above for more information.

create project for sonarqube with the rest-api / web-api

we try to automate the creation of projects (including user/group Management) in sonarqube and I already found the Web-API-documentation in our sonarqube 5.6-Installation. But if I try to create a project with the following settings
JSON-File create-project.json:
{"key": "test1", "name": "Testprojekt1"}
curl-request
curl --noproxy '*' -D -X POST -k -u admin:admin -H 'content-type: application/json' -d create_project.json http://localhost:9000/api/projects/create
I get the Error:
{"err_code":400,"err_msg":"Missing parameter: key"}
It's a bit strange because if I try e.g. the URL:
http://localhost:9000/api/projects/index
I get the list of the projects I created manuelly and if I try a request like
curl -u admin:admin -X POST 'http://localhost:9000/api/projects/create?key=myKey&name=myProject'
it works too, but I would like to use the new api because it looks like it support much more function that the 4.X API of sonarqube.
Maybe someone here can help me with this problem, if would very thanksful for every useful hint.
best regards
Dan
I found this question because I got the same "parameter missing" error message.
So what we both did not understand: The SQ API expects the parameters as plain URL parameters and not as json formatted parameters as most REST APIs do today.
PS: Would be nice if this could be added to the SQ documentation.

Resources