How to set --data-binary and --compressed in Curb gem - ruby

I'd like to set --data-binary and --compressed options.
In curl:
curl 'http://test.url' --data-binary test_json --compressed
How do I set --data-binary and --compressed options in curb?

Maybe this will work:
ce = Curl::Easy.new("http://test.url")
ce.encoding = 'gzip'
ce.multipart_form_post = true
fields = [
Curl::PostField.file('foo', foo_file_path),
Curl::PostField.file('bar', bar_file_path)
]
curl.http_post(fields)
ce.perform
the response body is in ce.body_str

Related

How to pass a string variable which has space characters to curl command in windows cmd

How to pass a string with whitespaces characters to a variable to curl command in windows cmd?
I have to pass a string variable that has white space characters to curl requests in windows cmd.
Below is the command.
set emailID="xxxx#gmail.com"
set openAPISpec="http://petstore/v2/swagger.json"
set licenseKey="ccc0e4-b000-491f-9d7b-7e59f9768"
set projectName="Online Banking REST API OmPf"
set MyDefault="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyDefault"
set MyRoleAdmin="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleAdmin"
set MyRoleUser="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleUser"
curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" -d "{ \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"%projectName%\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"
When I run the above command in windows cmd below is the response
curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" -d "{ \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"%projectName%\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"
{"timestamp":"2022-10-19T03:03:39.094+00:00","status":400,"error":"Bad Request","path":"/api/v1/scan"}
The above command in windows CMD is working perfectly if the variable doesn't have whitespaces characters.
The same was the error I was getting in Linux bash, so I modified the above command accordingly to the Linux environment and it's working perfectly even for whitespaces characters in the variable.
Below is the working command in Linux if a variable has whitespaces characters in it.
export emailID="xxxx#gmail.com"
export openAPISpec="http://petstore/v2/swagger.json"
export licenseKey="ccc0e4-b000-491f-9d7b-7e59f9768"
export projectName="Online Banking REST API OmPf"
export MyDefault="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyDefault"
export MyRoleAdmin="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleAdmin"
export MyRoleUser="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleUser"
curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" --data-raw '{ "openAPISpec": "'${openAPISpec}'", "email": "'${emailID}'", "licenseKey": "'${licenseKey}'", "projectName": "'"${projectName}"'", "headers":["Authorization: Bearer '${MyDefault}'","Authorization: Bearer '${MyRoleAdmin}'","Authorization: Bearer '${MyRoleUser}'"] }'
Below is the actual thing that is working in Linux bash, I have added extra double quotations to handle whitespaces characters with the projectName variable, and with the email variable it's normal.
"projectName": "'"${projectName}"'"
"email": "'${emailID}'"
The same modification is not working in windows CMD or currently, I'm not able to figure out
#1 Tried to pass this way
\"projectName\": \"\'\"%projectName%\"\'\"
# Command
curl -s --location --request POST "https://api.ethicalcheck.apisec.ai/api/v1/scan" -H "Content-Type: application/json" -d "{ \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"\'\"%projectName%\"\'\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"
# Response
{"timestamp":"2022-10-19T03:03:39.094+00:00","status":400,"error":"Bad Request","path":"/api/v1/scan"}
#2 Tried to pass this way
\"projectName\": \"\"%projectName%\"\"
# Command
curl -s --location --request POST "https://api.ethicalcheck.apisec.ai/api/v1/scan" -H "Content-Type: application/json" -d "{ \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"\"%projectName%\"\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"
# Response
{"timestamp":"2022-10-19T03:03:39.094+00:00","status":400,"error":"Bad Request","path":"/api/v1/scan"}
So how do I pass a string with whitespaces characters as a variable to curl command in windows CMD?
This link why-is-no-string-output-with-echo-var-after-using-set-var-text-command-line helped in resolving the issue.
Thanks #Mofi
The below syntax worked for me.
set "emailID=xxxx#gmail.com"
set "openAPISpec=http://petstore/v2/swagger.json"
set "licenseKey=ccc0e4-b000-491f-9d7b-7e59f9768"
set "projectName=Online Banking REST API OmPf"
set "MyDefault=dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyDefault"
set "MyRoleAdmin=dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleAdmin"
set "MyRoleUser=dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleUser"
curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" -d "{ \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"%projectName%\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"

Passing array of values in Sinatra GET in a less verbose way

I know Sinatra is not super concise when it comes to this topic. To pass an array of values to a GET controller through query string I'd have to do:
curl -v -H 'ContentType: application/json' -H 'Accept: application/json' 'http://0.0.0.0:8848/my/test?param1[]=1&param1[]=2&param1[]=3'
isn't there a way to do something like:
curl -v -H 'ContentType: application/json' -H 'Accept: application/json' 'http://0.0.0.0:8848/my/test?param1[]=1,2,3'
without then having to split/manipulate the string to get the different values?
You can use one of the usual gem for sinatra: https://github.com/mattt/sinatra-param.
curl -v \
-H 'ContentType: application/json' \
-H 'Accept: application/json' \
'http://0.0.0.0:8848/my/test?param1=1,2,3'
get '/test' do
param :param1, Array
end

Curl method with authentification problem

I have a little problem with my bearer:
I have a config file with my variable bearer like this:
bearer = "*********"
And I have my script test.sh :
source /home/name/test/config
curl -X GET \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer $bearer' \https://.../api/vendors \-o
json_file/vendors.json
And then when I open my file vendors.json. It say token invalid or was expired and normaly my token is good, I'm sure. So Where is my problem ?
Thanks

curl: 400 Bad Request. The browser (or proxy) sent a request that this server could not understand

I'm getting "400 Bad Request. The browser (or proxy) sent a request that this server could not understand." for the curl command. Curl command is as below, can someone please help?
token=jfkdjfdikdjydve83hhd54rsfdghsghktwhdh87
payload_file=test_fortify_scan.zip
curl -X POST https://dnsname/api/scan \
-H "authorization: Bearer $token" \
-H 'content-type: application/json' \
-d '{"projVer": "dev", "language": "java",
"payloadFile":"$payload_file", "emailList": "myemail#mail.com"}'
Looks like quotes problem. Look at Using curl POST with variables defined in bash script functions
token="jfkdjfdikdjydve83hhd54rsfdghsghktwhdh87"
payload_file="test_fortify_scan.zip"
curl -X POST https://dnsname/api/scan \
-H "authorization: Bearer $token" \
-H 'content-type: application/json' \
-d '{"projVer": "dev", "language": "java",
"payloadFile":"'"$payload_file"'", "emailList": "myemail#mail.com"}'

How do i add binary-data to curb POST

I'm trying to do the following POST to Parse Cloud using the Curb gem
curl -X POST \
-H "X-Parse-Application-Id: PARSE_APP_ID" \
-H "X-Parse-REST-API-Key: PARSE_API_KEY" \
-H "Content-Type: image/jpeg" \
--data-binary '#myPicture.jpg' \
https://api.parse.com/1/files/pic.jpg
with this:
curl = Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")
curl.multipart_form_post = true
curl.headers["X-Parse-Application-Id"] = PARSE_APP_ID
curl.headers["X-Parse-REST-API-Key"] = PARSE_API_KEY
curl.headers["Content-Type"] = "image/jpg"
res = curl.http_post(Curl::PostField.file('file', image.path))
Upload goes through with a 201, but it doesn't seem like the file makes it up to the server correctly.
Figured it out:
curl = Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")
curl.headers["X-Parse-Application-Id"] = PARSE_APP_ID
curl.headers["X-Parse-REST-API-Key"] = PARSE_API_KEY
curl.headers["Content-Type"] = "image/jpeg"
data = File.read('/Users/haider/Pictures/lion.jpg')
curl.post_body=data
curl.http_post
puts curl.body_str

Resources