Use Variable in CURL GET - json

I feel that I am just missing something very silly and not quoting/escaping something as I should, but I've been reading and testing for a solid amount of time now, and just can't get this to work.
I have a CSV file with data like the below:
TESTING.csv
17A3120UAXF-AA002771,9911017093
S150Y52157201,9911008933
17A3120UAXF-AA004545,9911016519
S170Y13084226,9911024365
S160Y45021270,9911018486
For the first part of my script, I need to read the second variable (ie. the items starting with "99110...")
I am parsing this data into a CURL while loop to sequence through all lines in the file. My bash script is like the below at this time:
#!/bin/bash
while IFS=, read -r device account; do
echo "device : $device"
echo "account : $account"
curl -X GET --header "Content-Type: application/json" --user username:password --verbose 'https://www.website.com:8444/api/subscriber?account=$account'
done < TESTING.csv
The issue that I'm running into is that while the "echo" statements are able to correctly pull/show the variable that I'm wanting to pass, this same information is not being passed into my CURL commands. When I run my script, the output is like the below:
device : 17A3120UAXF-AA002771
account : 9911017093
* About to connect() to www.website.com port 8444 (#0)
* Trying 8.8.8.8...
* Connected to www.website.com (8.8.8.8) port 8444 (#0)
* Server auth using Basic with user 'username'
> GET /api/subscriber?account=$account HTTP/1.1
> Authorization: Basic madeUpJunkAndNumbers12345==
> User-Agent: curl/7.29.0
> Host: www.website.com:8444
> Accept: */*
> Content-Type: application/json
>
< HTTP/1.1 404 Not Found
< Content-Type: application/json
< Content-Length: 0
<
* Connection #0 to host www.website.com left intact
^C

You are using single quotes int the URL, so the variables don't get expanded. Use double quotes:
curl -X GET --header "Content-Type: application/json" --user username:password --verbose "https://www.website.com:8444/api/subscriber?account=$account"

Related

Sending JSON file using relative path with curl

I have a JSON file called people.json:
[
{
"name": "Adam"
},
{
"name": "Eve"
}
]
I'm trying to send this data using a POST request with curl, from the project's root directory:
curl --request POST \
--header 'Content-Type: application/json' 'Accept: application/json' \
--data-binary #src/test/java/com/spring/app/people.json \
http://127.0.0.1:8080/api/v1/person -v -s
I keep getting the "Bad Request" error:
* Closing connection -1
* Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> POST /api/v1/person HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.74.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 80
>
* upload completely sent off: 80 out of 80 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 400
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Sat, 12 Dec 2020 03:56:38 GMT
< Connection: close
<
* Closing connection 0
{"timestamp":"2020-12-12T03:56:38.822+00:00","status":400,"error":"Bad Request","message":"","path":"/api/v1/person"}%
I've already had a look at this post, but still get the same error.
I've also installed homebrew curl, but even using that doesn't resolve the error.
I would appreciate any help.

error":"Unprocessable Entity","message":"Server could not parse JSON"

enter image description hereI am running below curl code to verify a user in Sandox Zendesk environment but it is giving me error "Unprocessable Entity","message":"Server could not parse JSON". Could anyone advise what is issue here?
curl https://abccompany.zendesk.com/api/v2/users/create_or_update_many.json \
-d '{"users": [{"name": "User2", "email": "user2#testuser.com", "verified": true}]}' \
-H "Content-Type: application/json" -X POST \
-v -u myemail#gmail.com:Password
I have attached the screenshot of the error. Also below are some of the details of the same error:
{"error":"Unprocessable Entity","message":"Server could not parse JSON"}* Connection #0 to host abccompany.zendesk.com left intact
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: /
* Could not resolve host: \
* Closing connection 1
curl: (6) Could not resolve host: \
curl: (3) [globbing] bad range specification in column 2
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: User2,/
* Could not resolve host: User2,
* Closing connection 2
curl: (6) Could not resolve host: User2,
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: email:/
* Could not resolve host: email
* Closing connection 3
What I mean is create a file in the current directory called request.json with the following content:
{"users": [{"name": "User2", "email": "user2#testuser.com", "verified": true}]}
And run the following command line:
curl https://abccompany.zendesk.com/api/v2/users/create_or_update_many.json -d #request.json -H "Content-Type: application/json" -v -u myemail#gmail.com:Password
Substituting your own e-mail and password.

CURL to POST to JIRA

I am using curl command in powershell in my Windows machine. I am trying to create an issue in JIRA which I have installed in my local. I tried following but it throws me error. Can someone let me know what am I missing and how to fix it?
PS C:\Users\raji> **curl -D- -u raji:raji -X POST --data $parse.json -H
"Content-Type: application/json" http://localhost:8080/rest/api/2/issue**
*curl: (6) Could not resolve host: Content-Type
HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
X-AREQUESTID: 770x749x1
X-ASEN: SEN-L8183526
Set-Cookie: JSESSIONID=D0B4391C94413FDDB1291C419F3E1360; Path=/; HttpOnly
X-Seraph-LoginReason: OK
Set-Cookie: atlassian.xsrf.token=B3EL-GHY4-P1TP-IMD0|d3d735e0a6566f8a97f99c96e80042551def3192|lin; Path=/
X-ASESSIONID: 1v4terf
X-AUSERNAME: raji
X-Content-Type-Options: nosniff
Content-Type: text/html;charset=UTF-8
Content-Length: 0
Date: Sun, 10 Jul 2016 07:20:36 GMT
*
When I try following I get this error:
PS C:\Users\raji> **curl -D- -u raji:raji -X POST --data #parse.json -H
"Content-Type: application/json" http://localhost:8080/rest/api/2/issue**
*At line:1 char:38
+ curl -D- -u raji:raji -X POST --data #parse.json -H "Content-Type: ap ...
+ ~~~~~~
The splatting operator '#' cannot be used to reference variables in an expression. '#parse' can be used only as an argument to a command. To reference variables in an expression use '$parse'.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : SplattingNotPermitted*
And hence I tried $ instaed of # in file name.
"parse.json" File has following content:
{
"fields": {
"project":
{
"key": "Demo"
},
"summary": "REST ye merry gentlemen",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}
}
}
As this is windows machine, I also tried using slash (/) in parse.json file (saw in few posts that / will remove the error) but that also did not help. Please can someone let me know how to fix this?
In the first case
curl -D- -u raji:raji -X POST --data $parse.json -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue
$parse is interpreted by powershell as variable, $parse.json as attribute json of the variable $parse, which does not exist, so the command executed would be
curl -D- -u raji:raji -X POST --data -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue
data is -H, and the content type header is interpreted as url to access.
In the second case the # in powershell is interpreted as splat operator, if you want a literal # (which is interperted by curl and not by powershell), simply quote the string:
curl -D- -u raji:raji -X POST --data "#parse.json" -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue
Now it should use the contents of the file parse.json as data.

fiware-orion NGSIv2 update context

I created a small stack using with orion and the populated mongodb from the tour guide app.
I don't understand why the updates queries are not working :(
if I query the context:
curl -s -X GET -H "Accept: text/plain" -H "fiware-service: tourguide" 'http://localhost:1026/v2/entities/0115206c51f60b48b77e4c937835795c33bb953f/attrs/capacity/value'
I get correctly the value "50"
if I update the value, following the query examples:
curl -s -v -X PUT -H "Accept: text/plain" -H "fiware-service: tourguide" -H "Content-Type: text/plain" 'http://160.85.2.22:1026/v2/entities/0115206c51f60b48b77e4c937835795c33bb953f/attrs/capacity/value' -d 52
i get error "The requested entity has not been found. Check type and id"
* Trying 160.85.2.22...
* Connected to 160.85.2.22 (160.85.2.22) port 1026 (#0)
> PUT /v2/entities/0115206c51f60b48b77e4c937835795c33bb953f/attrs/capacity/value?type=Restaurant HTTP/1.1
> Host: 160.85.2.22:1026
> User-Agent: curl/7.47.0
> Accept: application/json
> fiware-service: tourguide
> Content-Type: text/plain
> Content-Length: 2
>
} [2 bytes data]
* upload completely sent off: 2 out of 2 bytes
< HTTP/1.1 404 Not Found
< Connection: Keep-Alive
< Content-Length: 95
< Content-Type: application/json
< Fiware-Correlator: 9d2f4164-48f3-11e6-af87-0242ac110004
< Date: Wed, 13 Jul 2016 12:16:23 GMT
<
{ [95 bytes data]
* Connection #0 to host 160.85.2.22 left intact
{
"description": "The requested entity has not been found. Check type and id",
"error": "NotFound"
}
As far as I understand, you are using the context data of the FIWARE Tour Guide Application. In that context data, Restaurant entities belong to different service paths. In particular, each Resturant belong to a service path corresponding to the value of its department attribute.
Thus, have a look to the department attribute of the 0115206c51f60b48b77e4c937835795c33bb953f entity (using the Fiware-Service header: "tourguide"). If the value of the attribute is for example "Franchise4" then you have to use the following service path header in your PUT request (pay attention to the initial /):
-H "fiware-servicepath: /Franchise4"
Why GET request on attribute value is working without service path header while PUT request on attribute value isn't? When the header is omitted, query requests default to /# (which means "any service path") while create/udpate requests default to / (which is the root service path, which doesn't match with /Franchise4).

JSON Post data empty - Linux curl Command

I am trying to post json data to php file using linux curl command, (Lamp Server)
$ curl -V -H "Content-Type: application/json" -X POST -d '{"id": "123"}'
http://localhost/crm/UpdateUser.php
In UpdateUser.php,
<?php echo var_dump ($_POST);?>
OUTPUT:
[ec2-user#ip-10-35-1-181 ~]$ curl -v -H "Content-Type: application/json" -X POST -d '{"id": "123"}' http://viacrm.odema.net/crm/UpdateUser.php
* Hostname was NOT found in DNS cache
* Trying 54.217.206.217...
> POST /crm/UpdateUser.php HTTP/1.1
> User-Agent: curl/7.36.0
> Host: 192.168.1.16
> Accept: */*
> Content-Type: application/json
> Content-Length: 13
>
* upload completely sent off: 13 out of 13 bytes
< HTTP/1.1 200 OK
< Date: Mon, 16 Jun 2014 12:25:00 GMT
* Server Apache/2.2.27 (Amazon) is not blacklisted
< Server: Apache/2.2.27 (Amazon)
< X-Powered-By: PHP/5.3.28
< Content-Length: 13
< Connection: close
< Content-Type: text/html; charset=UTF-8
<
array(0) {
}
* Closing connection 0
Always the Post data shows empty, I even tried to use "ACCEPT: application/json", still same problem. Please can anyone guide this ?
$_POST only contains the results of decoding an application/x-www-form-urlencoded request. You need to read the raw request body. If you have the always_populate_raw_post_data configuration directive turned on, then the raw body will be in $HTTP_RAW_POST_DATA; otherwise you can obtain it by reading from the php://input stream.
Instead of $_POST try it:
<?php
print($HTTP_RAW_POST_DATA);
?>