Scalaj Data Urlencode - scala

When I execute a POST request using curl, it looks this way:
curl -k -X POST \
--header "Content-Type: application/x-www-form-urlencoded" \
--header "Accept: application/json" \
--data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" \
--data-urlencode "apikey=<somekey>" \
"https://iam.bluemix.net/identity/token"
In the scalaj-http library, I know we can add header, but I don't see a way to add data-urlencode as an option. How can I add this? I need it for my POST Request to be successful.

Try postForm like so
Http("https://iam.bluemix.net/identity/token")
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Accept", "application/json")
.postForm(Seq(
"grant_type" -> "urn:ibm:params:oauth:grant-type:apikey",
"apikey" -> "somekey"))
.asString

Related

ifttt webhook returns 403 (it works with curl)

I want to automatize a task, and the last step is to trigger a webhook on github. I am using ifttt "Make a web request"
This request works with curl
curl --location --request POST 'https://api.github.com/repos/wiso/TemperaturaUfficio/dispatches?event_type=webhook' \
--header 'Accept: application/vnd.github+json' \
--header 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXX' \
--header 'Content-Type: application/json' \
--data-raw '{
"event_type": "test_webhook"
}'
I am trying to do the same with ifttt:
I get:
Your server returned a 403. Unable to make web request to https://api.github.com/repos/wiso/TemperaturaUfficio/dispatches?event_type=webhook

Unable to update couchbase document using POST REST API

I want to update a couchbase document with REST API
ilceabcd1233.corp.abcd.com:8091/pools/default/buckets/{bucketName}/docs/{documentId}
When I hit below CURL command in postman, I receive 200 OK response Code with response as blank json Array: []
CURL:
curl --location --request POST 'ilceabcd1233.corp.abcd.com:8091/pools/default/buckets/{bucketName}/docs/{documentId}' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic QWRtaW5pc3RyYXRvcjpBZG1pbmlzdHJhdG9yff' \
--data-raw '{"name": "Nisarg", "age": 50}'
When I retrieve this document by below CURL:
curl --location --request GET 'ilceabcd1233.corp.abcd.com:8091/pools/default/buckets/{bucketName}/docs/{documentId}' \
--header 'Accept: application/json' \
--header 'Authorization: Basic QWRtaW5pc3RyYXRvcjpBZG1pbmlzdHJhdG9yff'
it responds:
{
"meta": {
"id": "112176152456",
"rev": "4-1637ac65ed7900000000000002000006",
"att_reason": "invalid_json",
"expiration": 0,
"flags": 33554438
},
"base64": "",
"xattrs": {}
}
On Couchbase web console I see message:
"Binary document. Base64 not available"
Can any one please help, what I am doing wrong ?
The trick here is that this API doesn't actually accept JSON. It's looking for application/x-www-form-urlencoded. Otherwise it will assume you are storing a binary document. You actually need a form value, which itself contains JSON. For example:
curl --location --request POST 'http://localhost:8091/pools/default/buckets/demo/docs/doc1' \
--header 'Accept: application/json, text/plain, */*' \
--header 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
--header 'Authorization: Basic QWRtaW5pc3RyYXRvcjpwYXNzd29yZA==' \
--data-urlencode 'value={"foo": "bar"}'
And just to echo my comment, this is an undocumented, unsupported endpoint that's really meant for internal use only. It's recommended instead to use an SDK (like the Couchbase Java SDK, for instance) to read/write documents. The REST API is intended for Cluster management, not for CRUD.

How to delete Keycloak realm via REST API

I'm not able to delete realm via REST API on Keycloak 8.0.2.
My Keycloak runs on localhost:38080. I followed this documentation https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_realms_admin_resource, see "Delete the realm" paragraph. First, I got token for admin user at http://localhost:38080/auth/realms/master/protocol/openid-connect/token
curl --location --request POST 'localhost:38080/auth/realms/master/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=admin' \
--data-urlencode 'password=admin' \
--data-urlencode 'client_id=vga_tests' \
--data-urlencode 'grant_type=password'
Then I used the token and tried to delete the example realm by this call
curl --location --request DELETE 'localhost:38080/auth/Example' \
--header 'Authorization: Bearer <TOKEN HERE>' \
--header 'Content-Type: application/json' \
--data-raw ''
(Note: curl commands are exported Postman calls)
I got this 404 response
{
"error": "RESTEASY003210: Could not find resource for full path: http://localhost:38080/auth/Example"
}
What am I doing wrong?
The documentation is incorrect, the address I needed to send the DELETE request to is localhost:38080/auth/admin/realms/example
curl command:
curl --location --request DELETE 'localhost:38080/auth/admin/realms/example' \
--header 'Authorization: Bearer <TOKEN HERE>' \
--header 'Content-Type: application/x-www-form-urlencoded'
Alternatively, using the CLI interface (search for "deleting a realm"),
you can do:
$ kcadm.sh delete realms/example

Request to VSTS REST API only works on Postman

I'm trying to run this request
curl -X POST \
'https://*****.visualstudio.com/DefaultCollection/_apis/wit/wiql?=&api-version=1.0' \
-H 'authorization: Basic *****' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: *****' \
-d '{
"query": "SELECT [System.Id] FROM WorkItems"
}'
but I keep getting this error
{"count":1,"value":{"Message":"A value is required but was not present in the request.\r\n"}}
It works as expected on Postman, so I think the request and the server are OK.
I'm trying to follow the first example shown here: https://www.visualstudio.com/en-us/docs/integrate/api/wit/wiql
Am I missing something?
The URL is wrong, remove =& from the REST API url and the url will be like this:
https://*****.visualstudio.com/DefaultCollection/_apis/wit/wiql?api-version=1.

Box -> View API -> Can not authenticate to create session

in this site :https://developers.box.com/view/
I do follow the instruction in the example (use Postman or curl)
curl https://view-api.box.com/1/sessions \
-H "Authorization: Token YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"document_id": "ABC123"}' \
-X POST
YOUR_API_KEY is replaced by my api key
ABC123 is replaced by a my pdf file.
But I got result: {"message": "Unsupported media type 'application/json, application/x-www-form-urlencoded' in request.", "type": "error", "request_id": "1f3d91c9489247579c78e7ceaa5e67c8"}
Please help me.
Thank you
It looks like there is an issue with the spaces after your \ characters. If you try:
curl https://view-api.box.com/1/sessions \
-H "Authorization: Token APIKEY" \
-H "Content-Type: application/json" \
-d '{"document_id": "DOCID"}' \
-X POST
It should work.