how to curl with query string parameter in format of "filter[name]=foo" - http

Here is an api on my server:
https://server/api/v1/products?filter[name]=foo
this works well when I access it via browser, and it works well with the following two methods:
$ cat /tmp/x
{"filter": {"name": "foo"}}
$ curl -X GET -H 'Accept: application/json' -k -s --negotiate -u : -H 'Content-Type: application/json' -d "#/tmp/x" https://server/api/v1/products
{"name": "foo", ...}
$ curl -X GET -H 'Accept: application/json' -k -s --negotiate -u : -d "filter[name]=foo" https://server/api/v1/products
{"name": "foo", ...}
Now I'm trying to do it with:
$ curl -X GET -H 'Accept: application/json' -k -s --negotiate -u : https://server/api/v1/products?filter[name]=foo
$ echo $?
3
it fails with error code 3, which stands for "URL malformed. The syntax was not correct."
How should I specify the query string parameters in the url?

The query string needs to be URL encoded so it has to look like this:
https://server/api/v1/products?filter%5Bname%5D=foo
asciitable.com.

Related

Change project key with bitbucket rest api

I am trying to write a script to move repos in a project to another project but I am getting a 400 error whenever I try.
My python requests line looks like:
url = 'https://bitbucketserver.com/rest/api/1.0/projects/example1/repos/repo1'
token = 'TokenString'
response = requests.put(url, headers={'Content-Type': 'application/json', 'Authorization': 'Bearer' + token}, data={'project': {'key': 'NEW_PROJECT'}}, verify=False)
I get a response 400 that says 'Unexpected character ('p' (code112)): expected a valid value (number, string, array, object, true, false, or null) at [Source: com.atlassian.stash.internal.web.util.web.CountingServletInputStream#7ccd7631; line 1, column 2]
I'm not sure where my syntax is wrong
Not python, but work for me via curl:
curl -u 'USER:PASSWORD' --request PUT \
--url 'https://stash.vsegda.da/rest/api/1.0/projects/OLD_PROJECT/repos/REPO_TO_MOVE' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"project": {"key":"NEW_PROJECT"}
}'
Maybe can someone help.

helm hosted repo via CLI

I installed the helm repo plugin for nexus3
Now I want to create the helm hosted repo via RestAPI command, in the same way as I do for raw repository
# RAW Repository
curl -X POST "${NEXUS_URL}/service/rest/v1/script" \
--user "admin:admin123" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{ \"name\": \"create_raw_repo\", \"content\": \"repository.createRawHosted(args, 'default')\", \"type\": \"groovy\"}"
curl -X POST "${NEXUS_URL}/service/rest/v1/script/create_raw_repo/run" \
--user "admin:admin123" \
-H "accept: application/json" \
-H "Content-Type: text/plain" \
-d "raw-release"
# HELM Repo
curl -X POST "${NEXUS_URL}/service/rest/v1/script" \
--user "admin:admin123" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{ \"name\": \"create_helm_repo\", \"content\": \"repository.createHelmHosted(args, 'default')\", \"type\": \"groovy\"}"
curl -X POST "${NEXUS_URL}/service/rest/v1/script/create_helm_repo/run" \
--user "admin:admin123" \
-H "accept: application/json" \
-H "Content-Type: text/plain" \
-d "helm-demo-release"
The problem is that the repository.createHelmHosted method doesn't exists.
What is the correct way to do it?
From version 3.21 nexus support OOB the helm repo and has REST API to create the repo
http://you-nexus:8081/service/rest/beta/repositories/helm/hosted
curl -X POST "http://you-nexus:8081/service/rest/beta/repositories/helm/hosted" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{ \"name\": \"internal\", \"online\": true, \"storage\": { \"blobStoreName\": \"default\", \"strictContentTypeValidation\": true, \"writePolicy\": \"allow_once\" }, \"cleanup\": { \"policyNames\": \"weekly-cleanup\" }}"

Send MSGPack body generates HTTP 500

I actualy try to replicate a http request with a msgpack body request. I have the original request that works (several times testet, generates a http 200 statuscode)
If i replicate the request for example in insomnia or in PowerShell or with curl i get an http error 500. I tried it with a binary file also as Body.
Any ideas?
Following curl command can be use:
curl --request POST \
--url https://4af7c20b-7646-4fb7-b64f-ae0a8c51c1f1.register.collector.scopely.io/register \
--header 'Accept: application/x-msgpack' \
--header 'Accept-Encoding: gzip' \
--header 'Cache-Control: no-cache' \
--header 'Connection: Keep-Alive' \
--header 'Content-Type: application/x-msgpack' \
--header 'Host: 4af7c20b-7646-4fb7-b64f-ae0a8c51c1f1.register.collector.scopely.io' \
--header 'User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.1.1; SM-N950N Build/NMF26X)' \
--header 'X-Unity-Version: 5.6.4p3' \
--header 'cache-control: no-cache' \
--header 'content-length: 672' \
--header 'x-scopely-titan-auth: 4af7c20b-7646-4fb7-b64f-ae0a8c51c1f1' \
--body 3gAQrHN5cy5wbGF0Zm9ybadhbmRyb2lkqXN5cy5zdG9yZaZHb29nbGW3c3lzLmNsaWVudF9nZW5lcmF0ZWRfaWTZIGQ3NDYyNDJhMzk3NjQ0N2E5ZTg3NmRlNmUxNDViZTgzr3N5cy5zZGtfdmVyc2lvbqUzLjAuN6pzeXMuYnVuZGxltGNvbS5zY29wZWx5LnN0YXJ0cmVrr3N5cy5hcHBfdmVyc2lvbqswLjU0My4xMDQ0Na5zeXMub3NfdmVyc2lvbr41LjEuMS9hcGktMjIobm1mMjZ4LzUwMDE5MDMzMSmpc3lzLm1vZGVssHNhbXN1bmcgU00tTjk1ME6qc3lzLmxvY2FsZaVkZV9ERalzeXMuYnVpbGSqUHJvZHVjdGlvbqtzeXMuY2FycmllcrB2b2RhZm9uZSBkMiBnbWJoqHN5cy5pbWVprzM1MTU2NDA4MDIwMjIyOapzeXMuc2VyaWFssDQ5NWE2ZGQzODk4YmM1Zmauc3lzLmFuZHJvaWRfaWSwYmM1ZmY0OTVhNmRkMzg5OK9zeXMubWFjX2FkZHJlc3OxQkM6NUY6RjQ6OTU6QTY6RES5c3lzLmdvb2dsZV9hZHZlcnRpc2luZ19pZNkkZTRiMThhN2EtOGQ2Yi00ZTFiLTgwYWItOWM5MzY4NGVkNTBm

Configuring a bitbucket repository to "activate" pipelines

I have multiple repositories in a BitBucket project.
I wish to automatically create a bitbucket repository, and enable pipelines (setting the pipeline configuration should be easy, with pushing a bitbucket-pipelines.yml file).
How can I do it using the REST API?
The other answer's "enable pipelines" request did not work for me.
This is what worked:
curl -X PUT -is -u '<username>:<password>' -H 'Content-Type: application/json' \
https://api.bitbucket.org/2.0/repositories/<username>/<slug>/pipelines_config \
-d '{
"enabled": true
}'
You can create a repository with the BitBucket REST API.
$ curl -X POST -H "Content-Type: application/json" -d '{
"scm": "git",
"project": {
"key": "Foo"
}
}' https://api.bitbucket.org/2.0/repositories/<username>/<repo_slug>
Push your bitbucket-pipelines.yml to your created repo.
curl https://api.bitbucket.org/2.0/repositories/<username>/<slug>/src \
-F /bitbucket-pipelines.yml=#bitbucket-pipelines.yml
Then enable pipeline for your project
curl -X PUT -is -u '<username>:<password>' -H 'Content-Type: application/json' \
https://api.bitbucket.org/2.0/repositories/<username>/<repo_slug> \
-d '{
"enabled": true,
"type": "repository_pipelines_configuration"
}'
Finally, you can trigger a pipeline for the branch like so.
$ curl -X POST -is -u <username>:<password> \
-H 'Content-Type: application/json' \
https://api.bitbucket.org/2.0/repositories/<username>/<slug>/pipelines/ \
-d '
{
"target": {
"ref_type": "branch",
"type": "pipeline_ref_target",
"ref_name": "<branch_name>"
}
}'
References:
Repository API
Pipelines API

How do I POST XML data with curl

I want to post XML data with cURL. I don't care about forms like said in How do I make a post request with curl.
I want to post XML content to some webservice using cURL command line interface. Something like:
curl -H "text/xml" -d "<XmlContainer xmlns='sads'..." http://myapiurl.com/service.svc/
The above sample however cannot be processed by the service.
Reference example in C#:
WebRequest req = HttpWebRequest.Create("http://myapiurl.com/service.svc/");
req.Method = "POST";
req.ContentType = "text/xml";
using(Stream s = req.GetRequestStream())
{
using (StreamWriter sw = new StreamWriter(s))
sw.Write(myXMLcontent);
}
using (Stream s = req.GetResponse().GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
MessageBox.Show(sr.ReadToEnd());
}
-H "text/xml" isn't a valid header. You need to provide the full header:
-H "Content-Type: text/xml"
I prefer the following command-line options:
cat req.xml | curl -X POST -H 'Content-type: text/xml' -d #- http://www.example.com
or
curl -X POST -H 'Content-type: text/xml' -d #req.xml http://www.example.com
or
curl -X POST -H 'Content-type: text/xml' -d '<XML>data</XML>' http://www.example.com
It is simpler to use a file (req.xml in my case) with content you want to send -- like this:
curl -H "Content-Type: text/xml" -d #req.xml -X POST http://localhost/asdf
You should consider using type 'application/xml', too (differences explained here)
Alternatively, without needing making curl actually read the file, you can use cat to spit the file into the stdout and make curl to read from stdout like this:
cat req.xml | curl -H "Content-Type: text/xml" -d #- -X POST http://localhost/asdf
Both examples should produce identical service output.
Have you tried url-encoding the data ? cURL can take care of that for you :
curl -H "Content-type: text/xml" --data-urlencode "<XmlContainer xmlns='sads'..." http://myapiurl.com/service.svc/
You can try the following solution:
curl -v -X POST -d #payload.xml https://<API Path> -k -H "Content-Type: application/xml;charset=utf-8"

Resources