cURL command to POST with data embedded in request - php

I am writing a simple shell script using which I will create a request in JIRA when conditions are met using curl. Following is the JIRA way of executing it, which needs me to send send the request as a command and parameters using a data file:
Request:
curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/
Data:
{
"fields": {
"project":
{
"key": "TEST"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}}}
So I don't want to use a separate data file as shown above. Rather I would like to using a single curl Request which has the data embedded in the request. Something like:
curl -D- -u fred:fred -X POST --"**PASS ALL MY DATA HERE**" -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/
I know it will make things cluttered, but thats the way i want it. Can you please suggest if the above is feasible.

Can you do it with a here document?
curl -D- -u fred:fred -X POST -d - -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/ <<EOF
{
"fields": {
"project":
{
"key": "TEST"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}}}
EOF

Related

How to send tweets with curl?

I need help because in my team we are using an old version of php and its impossible to use any bundle.
I'm trying to make the request with curl but Im having a lot of problems with authentication.
Could anyone tell me how to make a simple curl request to send a tweet using the twitter api?
Thanks a lot!!
curl -X POST 'https://api.twitter.com/2/tweets/search/stream/rules' \
-H "Content-type: application/json" \
-H "Authorization: Bearer $APP_ACCESS_TOKEN" -d \
'{
"add": [
{"value": "cat has:images", "tag": "cats with images"}
]
}'
it's just an example - but it's nicely described in the documentation

Sending json file through Http Post curl

I have been working with Jenkins, and now Im stuck when I try to make an HTTP Post using curl, I am sending a json file, to a file in my page who is waiting for it, and then do some functions, the code I am using is the following:
curl -X POST -k -i -H "Accept: application/json" -w "%{body}" -w "%{http_code}" -d "#/var/lib/jenkins/workspace/myFolder/session.json" http://mypage.com/myFolder/newfile.php
But apparently Im not sending correctly the data of the file, the newfile.php has something like this:
echo $_REQUEST['sessionId'];
But always get an error when I try to echo the index sessionId, I mean my file "newfile.php" is not getting anything from my jenkins curl, can you help me out?
Thanks in advance!!
The JSON that you send is in the request body. The keys/values will not be available in the $_REQUEST array, hence why you are getting the error. You will need to access the request body in order to get the sessionId property.
$requestBody = file_get_contents('php://input');
$session = json_decode($requestBody);
$sesionId = $session["sessionId"];
echo "Session ID: $sessionId";
The file_get_contents('php://input') returns the body of the POST request.
Make sure to also add the appropriate Content-Type header to your post request:
curl -X POST -k -i -H "Accept: application/json" -H "Content-Type: application/json" -w "%{body}" -w "%{http_code}" -d "#/var/lib/jenkins/workspace/myFolder/session.json" http://mypage.com/myFolder/newfile.php`

using curl in php with bearer authorization

im want to use curl with php to send push-messages to an ionic-app. this is my command im using on the commandline:
curl -X POST -H "Authorization: Bearer XXXXXX.AUTH.XXXXXXX" -H "Content-Type: application/json" -d '{
"tokens": ["XXXX.DEVICE_TOKEN.XXXX"],
"profile": "XXXX.PROFILE.XXXXX",
"notification": {
"message": "This is a test-message",
"title": "This is a Test Title"
}
}' "https://api.ionic.io/push/notifications"
how can i execute this command in php? i found some examples, but none of them works.

Symfont REST curl: (7) Failed connect to localhost:8000; No error

I am learning Symfony REST using this tutorial I found here
http://voryx.net/rest-apis-with-symfony2-the-easy-way/
I have gotten to the point where have to make a PUT using this command on my project's root console
curl -i -H "Content-Type: application/json" -X POST -d '{"name" : "Test Post", "description" : "This is a test post"}' http://localhost:8000/app_dev.php/api/posts
When I execute this command above, I get this error on my console
curl: (3) [globbing] unmatched close brace/brac
curl: (7) Failed connect to localhost:8000; No error
Please assist me I am pushing myself to learn Symfony REST

How to create the get method for the call

I am studying and implementing an api and while following the examples,
Here is the example 1 :
curl -X POST \
-H "Content-Type: application/json" \
-H "QuickBlox-REST-API-Version: 0.1.0" \
-d '{"application_id": "2", "auth_key": "DtF9cZPqTF8Wy9Q", "timestamp": "1333630580", "nonce": "1340569516", "signature": "13293a5bd2026b957ebbb36c89d9649aae9e5503", "user": {"login": "injoit", "password": "injoit"}}' \
https://api.quickblox.com/session.json
For the above example i consructed my get method as
https://api.quickblox.com/session.json?token=re8d22c6e617133ffeadd761193a6c57d87bfb1a0f&application_id=23995&auth_key=CbRasu4Wftu25Qw&nonce=8796&timestamp=1434446627&signature=667ee2b448a5d3dd57d112afef3f84dd6c67e165
and it is working good.
But for the below example
curl -X POST \
-H "QuickBlox-REST-API-Version: 0.1.0" \
-H "QB-Token: 17f6a337b0656c9c7e983f9705d79562fc694c0e" \
-H "Content-Type: application/json" \
-d '{"push_token": {"environment": "production", "client_identification_sequence": "aa557232bc237245ba67686484efab"},
"device": {" platform": "iOS", "udid": "5f5930e927660e6e7d8ff0548b3c404a4d16c04f"}}' \
http://api.quickblox.com/push_tokens.json
How can i create the get method. As this has two main arrays i am little confused.
Can anyone help in creating how can i use this call ?
It should be something like this:
https://api.quickblox.com/push_tokens.json?push_token[environment]=production&push_token[client_identification_sequence]=aa557232bc237245ba67686484efab&device[platform]=iOS&device[udid]=5f5930e927660e6e7d8ff0548b3c404a4d16c04f

Categories