I am trying to get the response of a Http post with curl in Jenkins, I have the following script:
curl -X POST -k -H "Accept: application/json" -H "Content-Type: application/json" --data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" http://mypage/Data/file.php
As you can see I am sending to file.php a json file, and then I am calling some functions and am returning a specific result.
With that script, I am getting the result I want, but I want to evaluate that result, let's say for example the result was "OK", then I want to assign the result to a variable, and then say if $result=="OK" then do this else do that. How can I do that, I have tried something like this:
if $response == "true" then exit 1 fi
But it does not seem to work out, does anyone know how it can be done?
They marked it as similar to this question
PHP cURL, extract an XML response
, but I don't see how, because I am not talking about php code, bash code, and I want to store the curl result in a variable....
Thanks in advance!!!
You can check the status code if you only need to check if the request was successful :
status=$(curl --write-out '%{http_code}' \
-s -o /dev/null \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php")
if [ "$status" == "200" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
with :
--write-out '%{http_code}' : output the status code
-o /dev/null : doesn't to output the body
-s : doesn't display connection log
As you have specified Accept: application/json, you expect a response in JSON format, so you could use jq JSON parser to parse it :
If the response is :
{ "status": true }
then you can do the following :
status=$(curl -s -H "Accept: application/json" \
-H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php" | jq -r '.status')
if [ "$status" == "true" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
If the response is not in JSON format and the response is OK :
status=$(curl -s -H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php")
if [ "$status" == "OK" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
Related
I have one problem regarding elastic search, I want to search the exact match in the filters. For example, If the color filter is applied only "Black" then the elastic search should be returned only "Black" products instead of other products which have Black keywords in their product color like "Black grey".
I have tried "match_phrase" instead of "match" but couldn't get anything.
Please check the screenshot of my JSON string: https://www.screencast.com/t/wjCcpfQwTxw
Thanks in advance
You can user term query.
For example:
GET {Your index}/_search
{
"query": {
"term": {
"color.keyword": {
"value": "Black"
}
}
}
}
It will return the documents which 'color' field equals to Black.If your color field is text type.Remember add '.keyword' after it.
To get an exact match use the type keyword for the field color. Here is an example with curl. Tested with Elasticsearch 7.3
Create an Index
curl -X PUT "localhost:9200/products?pretty" -H 'Content-Type: application/json' -d'
{}
'
Define an mapping for the field color of type keyword.
curl -X PUT "localhost:9200/products/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"color": {
"type": "keyword"
}
}
}
'
Add two sample data sets
*curl -X PUT "localhost:9200/products/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"color" : "blue green"
}
'
curl -X PUT "localhost:9200/products/_doc/2?pretty" -H 'Content-Type: application/json' -d'
{
"color" : "blue"
}
'*
Test the query.
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match": { "color": "blue" } } } '
When i send a request to onesignal with the following curl:
curl -X POST \
https://onesignal.com/api/v1/notifications \
-H 'Authorization: Basic <omitted>' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{
"app_id": "<omitted>",
"filters": [
{"field": "tag", "key": "broodjes", "relation": "exists"}
],
"contents": {"en": "English Message"}
}'
I'm trying to send a notification to all subscribed users with the tag "Broodjes".
Anywhere it says omitted I have omitted the value for obvious reasons.
Here is the unexpected response I get:
{
"id": "",
"recipients": 0,
"errors": [
"All included players are not subscribed"
]
}
And here is an image of the users I'm trying to notify.
It turned out that there was a trailing space on the end of my tag.
I'm trying to access access a URL using Curl, inn which I would like to status of the url, whether it is redirected, not found, suceess etc...
The code I'm using is working at certain times, but not always..
curl -u 98ur5t9#:9#98ur5t -v --silent --request GET "http://$url/laber.txt" --include 2>&1 | grep -q "HTTP/1.1 200 OK" 2> /dev/null
if [ $? -eq 0 ]
echo 'url ok'
else
echo 'url not oke'
fi
Can anyone help me with how do I get the correct status and if it is 200 echo ok as output..
curl -u 98ur5t9#:9#98ur5t -s -I http://www.example.org | grep -q "HTTP/1.1 200 OK"
if [ $? -eq 0 ]
echo 'url ok'
else
echo 'url not oke'
fi
Use --head to get only the response headers, something like:
response=$(curl --head -s -u 98ur5t9#:9#98ur5t GET "http://$url/laber.txt")
if [[ $response =~ 'HTTP/1.1 200 OK' ]]; then
echo 'url ok'
else
echo 'url NOT ok'
fi
I have got the following cURL command that is running expectedly:
curl -X POST
-H "Content-Type: application/json"
-d '{"duplicateEntitiesIds": [21,31,41]}'
{URL}:3003/v1/entities/5/merge
And I am trying to replicate that with Guzzle, which however fails, returning a 400 status code:
$request = $httpClient->post('{URL}:3003/v1/entities/'.$mainEntityId.'/merge',
['json' =>
['duplicateEntitiesIds' => $duplEntitiesIdsToArray]
]
);
$response = $request->send();
I have tried to change my post body , but it keeps failing. Any ideas would be appreciated.
NOTE
The data should be sent in the following format:
{"duplicateEntitiesId": [2,3,4]}
I'm using this curl command to send json data to a php webservice, but i'm not getting anything in the $_POST variable.
Here is the curl command
curl -X POST -i -H "Content-type: application/json" -c cookies.txt -X POST http://192.168.2.127:8888/json.php -d '{"age":"234","password":"password"}'
and here is the php code.
<?php
header('Content-type: application/json');
var_dump($_POST);
$return_arr = array();
$age = $_POST['age'];
$ageInt = intval($_POST['age']);
$return_arr['age1'] = $age;
var_dump($_POST);
$return_arr['age2'] = $ageInt;
echo json_encode($return_arr);
?>
Thanks in advance
Once try this
curl -X POST -H "Content-Type: application/json" -d '{"age":"21343","password":"password"}' http://192.168.2.127:8888/json.php