Laravel RESTful API NOT receiving data from cURL Request - php

So I have a "Skeleton" written for my projects' RESTful API. I've been trying to POST data to the store() function through cURL. And so I used
if(Input::hasfile('file'))
to check if the image/data was posting and it returned false. How would I actually detect the image/data if I were to post it via a cURL request similar to this
curl.exe --user "admin:password" --header "Content-Type: multipart/form-data" --data-binary "#vok.png" --output "out.txt" --dump-header "header.txt" http://bitdrops.co/bitdrops/public/index.php/api/v1/drop/
Ignore the header and response output.
This is my current code for the store() function.
public function store()
{
$file = Input::file('file');
$destinationPath = public_path() . '/uploads';
$filename = $file->getClientOriginalName();
//$extension =$file->getClientOriginalExtension();
$upload_success = Input::file('file')->move($destinationPath, $filename);
if( $upload_success ) {
return Response::json('success', 200);
} else {
return Response::json('error', 400);
}
}

The issue appears that in your cURL statement
curl.exe --user "admin:password" --header "Content-Type: multipart/form-data" --data-binary "#vok.png" --output "out.txt" --dump-header "header.txt" http://bitdrops.co/bitdrops/public/index.php/api/v1/drop/
You are not passing the file as "file". As you commented earlier on the solution is:
curl.exe -i --user user:pass -F file=#vok.png --output "out.html" http://bitdrops.co/bitdrops/public/index.php/api/v1/drop

Related

Get and evaluate curl response in Jenkins

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

cURL command to Guzzle

I need to convert a cURL command like this into Guzzle-way:
curl -XPOST "https://api/v1.0/endpoint" -F "file=#img.jpg"
This is what I'm trying so far:
$httpClient = new \GuzzleHttp\Client;
$req = $httpClient->createRequest('POST', $url), []);
$postBody = $req->getBody();
$postBody->addFile(new \GuzzleHttp\Post\PostFile('photo', fopen(storage_path() . '/' . $filename, 'r')));
$response = $httpClient->send($req);
But I'm not getting the same response as with the previous command, which is something like:
{"id":5378678,"url":"ui/54/68/97/24/img.jpg"}
I'm getting a GuzzleHttp\Message\Response object, but I'm not being able to find the id and url attributes in there.
Any help will be appreciated!
Just noticed I could do $response->json() to get what I wanted.

Send JSON data with Guzzle

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]}

How to specify --form parameter and PUT argument in php curl ?

I have to execute this curl command in php:
curl --digest -u YourApiKey:YourApiSecret "http://api.moodstocks.com/v2/ref/YourID" --form image_file=#"image.jpg" -X PUT
So far I have this:
function ms_addimage($file, $hash_id){
$postdata = array("image_file" => "#/".realpath($file));
$opts[CURLOPT_URL] = $this->API_BASE_URL . "ref/".$hash_id;
$opts[CURLOPT_VERBOSE] =1;
$opts[CURLOPT_POST] =true;
$opts[CURLOPT_POSTFIELDS] =$postdata;
$ch = curl_init();
curl_setopt_array($ch, $opts);
$raw_resp = curl_exec($ch);
echo "Response " . $raw_resp . "\n";
curl_close($ch);
}
The file path is correct but I am missing something.
How do I pass the --form parameter and the PUT argument?
So I was missing the following:
$opts[CURLOPT_CUSTOMREQUEST] = "PUT";
Now it is working.

Unable to get JSON data in _POST variable in php

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

Categories