Curl build with bash script - bash

I want to create curl with #!/bin/sh script and the part that giving me a headache is:
STR="-XGET -v -H 'Authorization: Bearer "$TOKEN"' https://my_endpoint/sth?"
echo $STR
CON="$(curl $STR)"
The echo there is:
-XGET -v -H 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJleHAiOjE0NTM5NzIxODcsInVzZXJuYW1lIjoid2luZ3UtY2xpZW50LWFkbWluQHNwZWljaGVyMjEwLmNvbSIsImlhdCI6IjE0NTM4ODU3ODcifQ.ADLcrV4yph6owwgMLak2RsnC95WK17ULflCisvNWBkeA93G4WUQ-BMrjnhQeuIgSfxSYnAmgmI36ggc2PytWhkqk8lIYrMJTH80tggBYCnnuA2lM26IZ2ViUMK1cj-BH3-dh4HmqSm_hozAFnVqGQi9P5J4CBz8eCf_mKc3iq-7EnXRikTkgakF69-jPfFA_9yO26JzZeDpymowa-LRPafWPtYinzmkaUQ2SHjUdWtGmELAyzkGUOOXrZ8TgvV9Yeb-OnEoY54GRSlb4ogVzAwWCJ2Y6vxmvNpAN5wiUZMylqTGhnqFr9MOp4JId1RavjwT7STRp9bCHBxD55CtYoEQ-oSpDv6WkgB07CtCRi0Spx9ErVsaB0Xf1mH9XKAVjOQ_dNNpKTxlqIXMbbosxEhjYE9K6Z30c3uWhCgccNdEEHxPhi7d2bRO3M_3fJPKsYWWk5DXhxmkFpJ4fLf05JO31FFIoj8q7H3c5NEvXVk_keS-jPY5iP5xRY1dv9P8bWPEwFk1-qQrXZ1mMNiLDLxdB9cXE9Tm6Eo4Rxo71H2o4Z1DHmnVHHctsATzywsJIe3o8Ym5o0OsmS3WH3EJ-IS572lFv-ZQSkt3fq927JlvWotd9HHMT2MOPf8Zg5cdNd-hclZUfj7qOi-a0Afbn2cT3FccBXJ8l4' https://my_endpoint/sth?
And when i copy this from terminal and paste it into curl it works, but in the verbose log I find:
-XGET -v -H 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJleHAiOjE0NTM5NzIxODcsInVzZXJuYW1lIjoid2luZ3UtY2xpZW50LWFkbWluQHNwZWljaGVyMjEwLmNvbSIsImlhdCI6IjE0NTM4ODU3ODcifQ.ADLcrV4yph6owwgMLak2RsnC95WK17ULflCisvNWBkeA93G4WUQ-BMrjnhQeuIgSfxSYnAmgmI36ggc2PytWhkqk8lIYrMJTH80tggBYCnnuA2lM26IZ2ViUMK1cj-BH3-dh4HmqSm_hozAFnVqGQi9P5J4CBz8eCf_mKc3iq-7EnXRikTkgakF69-jPfFA_9yO26JzZeDpymowa-LRPafWPtYinzmkaUQ2SHjUdWtGmELAyzkGUOOXrZ8TgvV9Yeb-OnEoY54GRSlb4ogVzAwWCJ2Y6vxmvNpAN5wiUZMylqTGhnqFr9MOp4JId1RavjwT7STRp9bCHBxD55CtYoEQ-oSpDv6WkgB07CtCRi0Spx9ErVsaB0Xf1mH9XKAVjOQ_dNNpKTxlqIXMbbosxEhjYE9K6Z30c3uWhCgccNdEEHxPhi7d2bRO3M_3fJPKsYWWk5DXhxmkFpJ4fLf05JO31FFIoj8q7H3c5NEvXVk_keS-jPY5iP5xRY1yirgabLv9P8bWPEwFk1-qQrXZ1mMNiLDLxdB9cXE9Tm6Eo4Rxo71H2o4Z1DHmnVHHctsATzywsJIe3o8Ym5o0OsmS3WH3EJ-IS572lFv-ZQSkt3fq927JlvWot5MN9HHMT2MOPf8Zg5c6udNd-hclZUfj7qOi-a0Afbn2cT3FccBXJ8l4' https://my_endpoint/sth?
* Rebuilt URL to: Bearer/
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Could not resolve host: Bearer
* Closing connection 0
curl: (6) Could not resolve host: Bearer
* Rebuilt URL to: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJleHAiOjE0NTM5NzIxODcsInVzZXJuYW1lIjoid2luZ3UtY2xpZW50LWFkbWluQHNwZWlja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fFA_9yO26JzZeDpymowa-LRPafWPtYinzmkaUQ2SHjUdWtGmELAyzkGUOOXrZ8TgvV9Yeb-OnEoY54GRSlb4ogVzAwWCJ2Y6vxmvNpAN5wiUZMylqTGhnqFr9MOp4JId1RavjwT7STRp9bCHBxD55CtYoEQ-oSpDv6WkgB07CtCRi0Spx9ErVsaB0Xf1mH9XKAVjOQ_dNNpKTxlqIXMbbosxEhjYE9K6Z30c3uWhCgccNdEEHxPhi7d2bRO3M_3fJPKsYWWk5DXhxmkFpJ4fLf05JO31FFIoj8q7H3c5NEvXVk_keS-jPY5iP5xRY1yirgabLv9P8bWPEwFk1-qQrXZ1mMNiLDLxdB9cXE9Tm6Eo4Rxo71H2o4Z1DHmnVHHctsATzywsJIe3o8Ym5o0OsmS3WH3EJ-IS572lFv-ZQSkt3fq927JlvWot5MN9HHMT2MOPf8Zg5c6udNd-hclZUfj7qOi-a0Afbn2cT3FccBXJ8l4'
* Closing connection 1
curl: (6) Could not resolve host: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJleHAiOjE0NTM5NzIxODcsInVzZXJuYW1lIjoid2luZ3UtY2xpZW50LWFkbWluQHNwZWljaGVyMjEwLmNvbSIsImlhdCI6IjE0NTM4ODU3ODcifQ.ADLcrV4yph6owwgMLak2RsnC95WK17ULflCisvNWBkeA93G4WUQ-BMrjnhQeuIgSfxSYnAmgmI36ggc
* Trying 54.228.198.226...
* Connected to my_endpoint (54.228.198.226) port 443 (#2)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.herokuapp.com
* Server certificate: DigiCert SHA2 High Assurance Server CA
* Server certificate: DigiCert High Assurance EV Root CA
> GET /api/sth? HTTP/1.1
> Host: my_endpoint/sth?
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 401 Unauthorized
< Connection: keep-alive
< Server: nginx/1.8.0
< Content-Type: application/json
< Transfer-Encoding: chunked
< X-Powered-By: PHP/5.6.17
< Cache-Control: no-cache
< Allow: GET
< Date: Wed, 27 Jan 2016 09:09:49 GMT
< Via: 1.1 vegur
<
{ [59 bytes data]
100 48 0 48 0 0 41 0 --:--:-- 0:00:01 --:--:-- 0
* Connection #2 to host my_endpoint left intact
What's wrong with that header?

Use an array to store command arguments.
curl_options=(-XGET -v -H "Authorization: Bearer $TOKEN" "https://my_endpoint/sth?")
content=$(curl "${curl_options[#]}")

You can use
eval curl $STR "https://my_endpoint" > 'output_file'

Related

can't use curl to query neo4j

I am trying to use curl to query neo4j
curl -X POST -H Accept:application/json -H Content-Type:application/json -u neo4j:password -v http://localhost:7474/db/neo4j/tx/commit -d '{"statements":[{"statement":"MATCH (n) RETURN n"}]}'
gives me this response
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 127.0.0.1:7474...
* Connected to localhost (127.0.0.1) port 7474 (#0)
* Server auth using Basic with user 'neo4j'
> POST /db/neo4j/tx/commit HTTP/1.1
> Host: localhost:7474
> Authorization: Basic bmVvNGo6cGFzc3dvcmQ=
> User-Agent: curl/7.79.1
> Accept:application/json
> Content-Type:application/json
> Content-Length: 47
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Wed, 27 Jul 2022 09:13:35 GMT
< Access-Control-Allow-Origin: *
< Content-Type: application/json
< Content-Length: 120
<
{"results":[],"errors":[{"code":"Neo.ClientError.Request.InvalidFormat","message":"Could not parse the incoming JSON"}]}* Connection #0 to host localhost left intact
If anyone could help please
Should have mentioned I'm on windows. Apparently you have to escape those double quotes in the json
This works for me now:
curl -X POST -H Accept:application/json -H Content-Type:application/json -u neo4j:password -v http://localhost:7474/db/neo4j/tx/commit -d "{\"statements\":[{\"statement\":\"MATCH (n) RETURN n\"}]}"

How to access Gravitee AM api

How to curl the basic gravitee am api? i tried the simple one also cant?
curl -X POST http://localhost:8093/admin/token -H 'authorization: Basic base64(admin:adminadmin)'
curl -X POST http://localhost:8093/admin/token -H 'Authorization: Basic YWRtaW46YWRtaW5hZG1pbg=='
It stated 404 Not found. Result are below:
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8093 (#0)
> POST /admin/token HTTP/1.1
> Host: localhost:8093
> User-Agent: curl/7.58.0
> Accept: */*
> Authorization: Basic YWRtaW46YWRtaW5hZG1pbg==
>
< HTTP/1.1 404 Not Found
< Cache-Control: must-revalidate,no-cache,no-store
< Content-Type: text/html;charset=iso-8859-1
< Content-Length: 0
<
* Connection #0 to host localhost left intact
First you have to request an access token:
curl -X POST http://localhost:8093/management/auth/token -u admin:adminadmin
You should receive this payload:
{"access_token":"eyJraWQiOiJkZWZ......TyJ37E","expires_at":"Mon Nov 29 15:06:12 CET 2021","token_type":"bearer"}
Then you can use this access_token to request the access management API.
curl -X GET http://localhost:8093/management/organizations/DEFAULT/environments/DEFAULT/domains/ -H'Authorization: Bearer eyJraWQiOiJkZWZ......TyJ37E'

Calling a tibco soap webservice using shell script

I am trying to calling a TIBCO soap based web service through shell script using curl. I am getting response as 200 ok but am not getting output in response.xml
Tibco webservice is deployed in three instances. I am trying to hit one of the instance and retrieve response but I am getting content-length as 0.
I tried using curl and wget to call webservice and retrieve response but am getting content-length as 0.
Below is the curl command:
curl -v --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction: "/ServiceImplV1.0/getInfo"/" --data #request2.xml http://sfapt-ap98t:4121/service/dataservice -o response.xml
request2.xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.greatclips.com/ghf/tck/service/v1.0">
<soapenv:Header/>
<soapenv:Body>
<v1:GetBaseLineRequest>
<!--Optional:-->
<UniqueRequestIdentifier>?</UniqueRequestIdentifier>
</v1:GetBaseLineRequest>
</soapenv:Body>
</soapenv:Envelope>
Response Hostname was NOT found in DNS cache
* Trying 10.215.13.217...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to dlfapt-ap98a (10.215.13.217) port 6121 (#0)
> POST /service/addataservice HTTP/1.1
> User-Agent: curl/7.37.0
> Host: dlfapt-ap98a:6121
> Accept: */*
> Content-Type: text/xml;charset=UTF-8
> SOAPAction: /ServiceImplV1.0/getBaseLineDetails/
> Content-Length: 323
>
} [data not shown]
Response from server:
* upload completely sent off: 323 out of 323 bytes
< HTTP/1.1 200 OK
< Content-Length: 0
< Date: Fri, 19 Jul 2019 17:57:45 GMT
* Server is not blacklisted
< Server:
<
100 323 0 0 100 323 0 19383 --:--:-- --:--:-- --:--:-- 35888
* Connection #0 to host dlfapt-ap98a left intact

Jenkins pipeline not finish tomcat war deploy

I want to deploy a war from Jenkins using Tomcat manager.
Here is what i am doing from command line :
curl -v -u user:pasword -T target/app.war "http://host:8180/manager/text/deploy?path=&update=true"
It takes a little bit time and works :
* Hostname was NOT found in DNS cache
* Trying ip...
* Connected to host.com (ip) port 8180 (#0)
* Server auth using Basic with user 'jenkins'
> PUT /manager/text/deploy?path=&update=true HTTP/1.1
> Authorization: Basic amVua2luczpLbzNEaUE=
> User-Agent: curl/7.38.0
> Host: host.com:8180
> Accept: */*
> Content-Length: 71682391
> Expect: 100-continue
>
< HTTP/1.1 100
* We are completely uploaded and fine
< HTTP/1.1 200
< Cache-Control: private
< Expires: Thu, 01 Jan 1970 00:00:00 UTC
< Content-Type: text/plain;charset=utf-8
< Transfer-Encoding: chunked
< Date: Tue, 14 Aug 2018 13:18:22 GMT
<
OK - Application déployée pour le chemin de contexte [/]
* Connection #0 to host host.com left intact
My problem is when i am execute this command in Jenkins Pipeline :
stage('Tomcat Deploy') {
sh "curl -v -u user:password -T app.war http://host:8180/manager/text/deploy?path=&update=true"
}
The curl command is not finished correctly :
+ curl -v -u jenkins:pass -T app.war http://host:8180/manager/text/deploy?path=
* Hostname was NOT found in DNS cache
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying ip...
* Connected to host.com (ip) port 8180 (#0)
* Server auth using Basic with user 'jenkins'
> PUT /manager/text/deploy?path= HTTP/1.1
> Authorization: Basic amVua2luczpLbzNEaUE=
> User-Agent: curl/7.38.0
> Host: host.com:8180
> Accept: */*
> Content-Length: 71682391
> Expect: 100-continue
>
< HTTP/1.1 100
} [data not shown]
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
And it goes to the next stage without waiting the curl deploy result. Is there any quick solution to fix that?
Yes the war was not uploaded. But i resolved my problem using --upload-file option. Now Jenkins wait for upload as attended. Thank you.
A quick(but very bad) fix for this would be to put a sleep for some time in your code.
sleep(30)
This will give curl commad time to submit/upload your jar to your target server. ** Dont rely on this in Production **

Curl post multipart file to spring boot rest endpoint

I am trying to post a file to a restful endpoint implemented in spring boot using Curl and it throws the following error:
$ curl -v http://localhost:8081/qas/uploadCsv -X POST -F "file=#test.csv"
Note: Unnecessary use of -X or --request, POST is already inferred.
* timeout on name lookup is not supported
* Trying ::1...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to localhost (::1) port 8081 (#0)
> POST /qas/uploadCsv HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.46.0
> Accept: */*
> Content-Length: 4257762
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------e16823418f4c8f54
>
< HTTP/1.1 100 Continue
} [155 bytes data]
< HTTP/1.1 400 Bad Request
< Server: Apache-Coyote/1.1
< X-Application-Context: application:8081
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Mon, 11 Apr 2016 14:27:42 GMT
< Connection: close
<
{ [247 bytes data]
100 4158k 0 236 100 4157k 1888 32.4M --:--:-- --:--:-- --:--:-- 32.4M{"timestamp":1460384862046,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required MultipartFile parameter 'file' is not present","path":"/qas/uploadCsv"}
* Closing connection 0
I must be missing something basic but cannot see what it is. It is looking for the request param 'file' and am not sure how to send this through Curl.
Spring java config has following beans:
#Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("1000MB");
factory.setMaxRequestSize("1000MB");
return factory.createMultipartConfig();
}
#Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSizePerFile(10000000);
return multipartResolver;
}
My spring boot web service is as follows:
#RequestMapping(value = "/uploadCsv", method = RequestMethod.POST, consumes = {"multipart/*"})
public #ResponseBody result handleFileUpload(#RequestParam("file") MultipartFile file) {
in the above signature it is looking for the requestParam file which correlates with the cUrl error.
"status":400,"error":"Bad Request",
"exception":"org.springframework.web.bind.MissingServletRequestParameterException",
"message":"Required MultipartFile parameter 'file' is not present","path":"/qas/uploadCsv"}
This request works in postman which submits the following request:
POST /qas/uploadCsv HTTP/1.1
Host: localhost:8081
Cache-Control: no-cache
Postman-Token: d62a469b-16c8-b30a-4168-7622d9695c57
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename=""
Content-Type:
----WebKitFormBoundary7MA4YWxkTrZu0gW
I also have a mockMvc integration test that works fine. Comparing the two (cUrl vs postman) in the cUrl there is no content disposition which contains the file param. Have looked around on google and cannot see to send this request? Any pointers
Thanks in advance
UPDATE:
I tried the solution in the following link
File upload working under Jetty but not under Tomcat
Using the same cURL command above the file is null throwing the error below
$ curl -v http://localhost:8081/qas/uploadCsv -X POST -F "file=#test.csv" -H "Content-Type: multipart/form-data"
Note: Unnecessary use of -X or --request, POST is already inferred.
* timeout on name lookup is not supported
* Trying ::1...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to localhost (::1) port 8081 (#0)
> POST /qas/uploadCsv HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.46.0
> Accept: */*
> Content-Length: 4257762
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------e6cd31736e52dfa2
>
< HTTP/1.1 100 Continue
} [155 bytes data]
100 4157k 0 0 100 4157k 0 681k 0:00:06 0:00:06 --:--:-- 0< HTTP/1.1 500 Internal Server Error
< Server: Apache-Coyote/1.1
< X-Application-Context: application:8081
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 12 Apr 2016 07:23:18 GMT
< Connection: close
<
100 4157k 0 0 100 4157k 0 622k 0:00:06 0:00:06 --:--:-- 0{ [4 bytes data]
100 4158k 0 174 100 4157k 26 622k 0:00:06 0:00:06 --:--:-- 0{"timestamp":1460445797961,"status":500,"error":"Internal Server Error","exception":"java.lang.NullPointerException","message":"No message available","path":"/qas/uploadCsv"}
* Closing connection 0
Curl POST + Multipart
To POST with a file, add this -F file=#"path/to/data.txt"
curl -F file=#"test.csv" http://localhost:8081/qas/uploadCsv
I think this is due to the fact that file here is not a parameter. According to Spring web, a parameter is a 'query' parameter, so for example /api?foo=bar, foo is here the parameter in this request.
So in your case, Spring complains about a missing parameter file because it expects the request to look like http://localhost:8081/qas/uploadCsv?file=something.
Changing the method signature from
handleFileUpload(#RequestParam("file") MultipartFile file)
to
handleFileUpload(#RequestPart("file") MultipartFile file)
should fix your issue

Resources