cURL POST --data-binary using content from URL? - post

Is it possible to use content from a URL to do Curl -data-binary POST?
POST content from a file:
curl -X POST -H "Content-Type: plain/text" --data-binary "#file.txt" http://somewhere.com
Is there a curl command can do like
curl -X POST -H "Content-Type: plain/text" --data-binary "http://somefile.com/file" "http://somewhere.com"
suppose http://somefile.com/file is a binary text file

you can do the following:
curl -s http://somefile.com/file | curl --data-binary #- http://somewhere.com

Related

Failure occurred during GIT Browsing

All, I am using REST API to add bitbucket branch URL into SAST scanner. When sending the REST API from postman, getting error message: "Failure occurred during GIT Browsing". The curl is:
curl --location --request POST 'http://<host name/IP>/cxrestapi/projects/{projectId}/sourceCode/remoteSettings/git' --header 'Authorization: Bearer --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'url=https://bitbucket.org//.git'
--data-urlencode 'branch=refs/heads/master' --data-urlencode 'username=' --data-urlencode 'password='
If I do the same for public Github (https://github.com//.git), curl works fine. Any suggestion is appreciated.

Slack CURL reminders.app getting internal_error

Trying to use the reminders.app to post reminders via curl.
Getting the following error
{"ok":false,"error":"internal_error","warning":"missing_charset","response_metadata":{"warnings":["missing_charset"]}}
curl -X POST -H 'Authorization: Bearer secretsxxxxxxxxxxxxx' -H 'Content-type: application/json' --data \
'{
"ok": true,
"reminder": {
"text": "eat a banana",
"recurring": false,
"time": 1602288000
}
}' https://slack.com/api/reminders.add
You need to change the following line https://slack.com/api/reminders.add to https://slack.com/api/reminders.add?. Include the question mark at the end.
So your code should be:
curl -X POST -H 'Authorization: Bearer secretsxxxxxxxxxxxxx' \
-H "Content-type: application/json" \
--data '{"text": "I hope you eat your banana","time":1581447960}' \
https://slack.com/api/reminders.add

How to get manifests using HTTP API v2?

How to authenticate with the V2 API is useful and works.
REPO="https://hub.docker.com/v2"
I'm able to get tokens, list (my) repos and lists their images and tags.
curl --silent \
--header "Authorization: JWT ${TOKEN}" \
${REPO}/repositories/${USERNAME}/
curl --silent \
--header "Authorization: JWT ${TOKEN}" \
${REPO}/repositories/${USERNAME}/${IMAGE}/tags/
I'd like to 'GET MANIFEST' but I'm struggling to get this to work:
https://docs.docker.com/registry/spec/api/#manifest:
curl --silent \
--header "Host: hub.docker.com" \
--header "Authorization: JWT ${TOKEN}" \
${REPO}/repositories/${USERNAME}/${IMAGE}/manifests/
curl --silent \
--header "Host: hub.docker.com" \
--header "Authorization: JWT ${TOKEN}" \
${REPO}/${USERNAME}/${IMAGE}/manifests/
curl --silent \
--header "Host: hub.docker.com" \
--header "Authorization: JWT ${TOKEN}" \
${REPO}/${USERNAME}/${IMAGE}/manifests/${TAG}
I've tried with|without the Host header. With various values for the Host header. But, I'm clearly missing something. I tried pattern-matching against the working endpoints but no joy:
curl --silent \
--header "Authorization: JWT ${TOKEN}" \
${REPO}/repositories/${USERNAME}/${IMAGE}/manifests/
Curiously, this page shows "GET TAGS" seemingly incorrectly as /v2/<name>/tags/list:
https://docs.docker.com/registry/spec/api/#tags
Reviewed:
https://stackoverflow.com/a/45605443/609290
Follow-up
I'm a Googler and have access to Google Container Registry (GCR).
REPO="https://gcr.io/v2/"
On a whim, I just tried 'GET MANIFEST' against GCR and the requests works:
curl --silent \
--request GET \
--user _token:$(gcloud auth print-access-token) \
${REPO}/${PROJECT}/${IMAGE}/manifests/${TAG}
It's quite confusing with all the *.docker.com|io subdomains!
I found registry.hub.docker.com and index.docker.io the most reliable ones.
You can easily query the tags from there, but for the manifests you'll need to get a token for pulling first:
REGISTRY=https://index.docker.io/v2
#REGISTRY="https://registry.hub.docker.com/v2"
#REGISTRY="https://registry.docker.io/v2"
#REGISTRY="https://registry-1.docker.io/v2"
#REGISTRY="https://hub.docker.com/v2"
REPO=library
IMAGE=debian
# Could also be a repo digest
TAG=latest
# Query tags
curl "$REGISTRY/repositories/$REPO/$IMAGE/tags/"
# Query manifest
curl -iL "$REGISTRY/$REPO/$IMAGE/manifests/$TAG"
# HTTP/1.1 401 Unauthorized
# Www-Authenticate: Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/debian:pull"
TOKEN=$(curl -sSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$REPO/$IMAGE:pull" \
| jq --raw-output .token)
curl -LH "Authorization: Bearer ${TOKEN}" "$REGISTRY/$REPO/$IMAGE/manifests/$TAG"
# Some repos seem to return V1 Schemas by default
REPO=nginxinc
IMAGE=nginx-unprivileged
TAG=1.17.2
curl -LH "Authorization: Bearer $(curl -sSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$REPO/$IMAGE:pull" | jq --raw-output .token)" \
"$REGISTRY/$REPO/$IMAGE/manifests/$TAG"
# Solution: Set the Accept Header for V2
curl -LH "Authorization: Bearer $(curl -sSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$REPO/$IMAGE:pull" | jq --raw-output .token)" \
-H "Accept:application/vnd.docker.distribution.manifest.v2+json" \
"$REGISTRY/$REPO/$IMAGE/manifests/$TAG"
See
this gist for another example and
this repo for a reusable script docker-image-size-curl.sh
Authorization with hub.docker.com works differently and you don't seem to get the manifests from there 🤔

Why am i getting HTTP 404 & sub_code":"S00004 ? IBM Watson

I am using curl command :
curl -X POST --user "apikey:xxx" \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
--data-binary #profile.json \
"https://gateway-fra.watsonplatform.net/personality-insights/api"
and IBM is giving me response:
x{"code":404,"sub_code":"S00004","error":"Not Found"}%
anyone has idea why?
this is my .json file
https://watson-developer-cloud.github.io/doc-tutorial-downloads/personality-insights/profile.json
Try using the following curl command:
curl -X POST --user "apikey:xxxxxxxx" --header "Content-Type: application/json" --header "Accept: application/json" --data-binary #profile.json "https://gateway-fra.watsonplatform.net/personality-insights/api/v3/profile?version=2017-10-13&consumption_preferences=true&raw_scores=true"
Replace xxxxxxxx with your apikey. Please avoid using real apikey in public in the future. Anyone can use your key. I have edited the question to hide it.
Please follow the API reference for Personality Insights for more info.

(string issues) How to do a curl POST call with a data string?

I am trying to call a curl command with the sh command, but I would get "errors parsing JSON" no matter what I try.
sh """
curl -s -X POST \
--url www.example.com
--data \"{\'state\': \'failure\'}\"
"""
Problem:
I think you added terminators incorrectly.
Solution:
Simply use this format. It should work fine.
curl -s -X POST --header "Content-Type: application/json" \
--request POST \
--data '{"state":"failure"}' \
http://www.example.com
The JSON standard requires double quotes around key value pairs. Looks like in your example your missing a \ after the --url as well.
Try:
sh '''
curl -s -X POST \
-H \'Content-type: application/json\' \
--url www.example.com \
--data \'{"state": "failure"}\'
'''
If you end up needing to use String interpolation then
sh """
curl -s -X POST \
-H 'Content-type: application/json' \
--url www.example.com \
--data '{\"state\": \"status\"}'
"""

Resources