Can I utilize cURL parameters without using cURL? - html

This is probably a stupid question but I'm pretty out of my depth here. I'm trying to utilize an API for my business, and while most of the API has the parameters in the form of "site?param1=one&param2=two", one of them does not. Instead it's in the form
required parameters -> key
optional parameters
params: a key-value array of where clauses for the query
I would love to be able to put the parameters in directly in the link, but I'm not sure how to parse this or if it's possible.
Example
https://thepetresorts.gingrapp.com/api/v1/animals?key=KEY&params=[{animal_id=1},{name=Charlie}]
This is the example they provided using cURL, but I'm really not interested in actually programming with the API, I just need to make specific pulls every once in a while, and I cannot for the life of me figure out how to utilize cURL.
curl "https://{your-subdomain-here}.gingrapp.com/api/v1/animals" \
-H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
--data-urlencode "params[month(from_unixtime(birthday))]=11" \
--data-urlencode "key={your-key-here}"
Any advice would be extremely appreciated!

If you're working with an API, it's even better if you can use POSTMAN or INSOMNIA.

The API your describing has two types of requests, GET and POST - the GET request is usually used to "get to view" but occasionally some use it to also modify and push data - this uses parameters within the URL (what you described), POST however is generally used to push data into the body to make a modification, the parameters are posted within the body of the request instead of in the actual URL like GET requests.
You won't be able to modify the endpoint (unless its your own) you will need to GET where its needed and POST when its needed, the POST request will likely deny a GET request as it's not the intended method - you can create a jump page (your own API) to accept GET parameters and generate a POST request to your API - as #muklis mentioned POSTMAN is great you can create the request in there, generate the code in any language and use that to produce a simple PHP page or so that'll take in $_GET[] variables and pass them into the generated POSTMAN request - it's probably your best bet.
-- Edit
You can also use Zapier, forgot about that - Zapier is amazing for easy code-less integrations; you can use the Webhook zap to receive your parameters in repost them in two easy steps within the zap.
Just another idea for you.

Related

How to process Vue/Axios Json payload posted data on Yii2

It took me a while to understand this, being that it was a little obvious. I will answer myself, so other can benefit of the answer and ofcourse to see if there's a better way to do this. The problem was based on Axios/Yii2 but I guess this will apply equally to other frontend libraries/frameworks sending data to Yii2.
I needed to post data from a small form made on Vuejs, sending the request Axios to a Action/Controller on Yii2, so data is sent on a simple POST request and the post is getting to the controller, but I was not able to receive the data on the action, $_POST | $post arrives empty (checked using xdebug).
As much as I remember, this had something to do with security. But I already tried by disabling public $enableCsrfValidation, so that was not the problem.
public $enableCsrfValidation = false;
But no matter what, data was not being added to the request/post data inside Yii2.
The following Image, explains the problem you will find there:
The Axisos method that sends the post with test data.
The Yii2 Action stpoed at the place, I should be able to see data.
The capture of the xdebug variables and data for the request.
The capture of Chrome where you can check the payload is sent.
The answer is as I said "kind of obvious", but I could not see that, and I am sure some other devs will probably fall on this.
After searching like crazy and asking everyone, I tried sending the request by using Postman app, yup the best thing I know to test apis.
Dont forgue to add the xdebug cookie to be able to debug your PHP Endpoint.
There I found the first clue «the obvious part», I was not sending data as a form-data, Axios and other libraries, send the data as a raw (application/json) payload.
This means that Yii2 will no be able to find the data inside the post request, yes its there but Yii2 magic will not work, neither you will find this data inside $GLOBALS or in $_POST.
So reading the Yii2 documentation I found that inside request I can use a function that will help me recovering the Raw data, so to do this use the following line:
$raw_data = Yii::$app->request->getRawBody();
Now, that data gets to you as a simple, raw json string, so use the power of PHP to parse it to an object.
$object= json_decode($raw_data );
And finally use the data inside by calling the properties you look for, sent on the pay load:
Json Payload:
{
"msg":"This is my payload",
"id":"11"
}
To use it:
echo $object->{'msg'}; // prints: This is my payload
So that's the way to handle that, now I would like some other points of view to see if there's a better way or cleaner way to do this. Hope it helps.

Send Multiple Parameters in REST GET call to resource

What's the best way to send multiple parameters on a REST GET resource call. Normally we can call GET call with path param &/ query however the number of character is limited on a URL so any suggestion or best practice on how to achieve this.
This can be achieved via POST where sending the query in request body as JSON and use json converter on the resource end. I am thinking POST mayn't be a right approach for query or get service from a resource.
I search the existing questions on this but didn't get any proper answer.
Thanks in advance.
You can send a limited data with GET and even the data is visible in URL making data vurnerable. When you use POST data is a alot more safer than GET and you can send large no. of request parameters. You can checkout this link

Perl : Json : Can I perform a GET within my POST processing?

In theory... is it possible to
parse a POST payload in which a url is supplied,
then use the supplied url to perform a GET to acquire data,
that data is used to query a db and
then send my response to the POST?
Or will my original POST request pick up the GET response and kill everything?
I can't seem to find an example or reference to this being done anywhere...
Thanks!
Yes, it's possible, and not just in theory. I've done much the same thing myself. Just use LWP (or your preferred HTTP client library) to perform the GET as usual. Whether you're doing it within a POST handler or not makes no difference.

Querying REST API using cURL

To begin with I am a novice, both with REST and cURL. This is what I want to do:
I want to query the Twitter Rest API using commandline (some bash script) and hence, I want to use cURL to do that.
I have been trying to search for some tutorial (with no luck) that explains how to use REST with cURL and how to form the headers, get/post and then how to receive the REST response and the response status, so that I can write my logic based on the response status.
I would greatly appreciate if you can give me a few examples on how to call the twitter REST API using curl and also how to access the REST response status.
I tried calling twitter using this:
curl "http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&screen_name=$user_name&count=150"
And got a json which I was able to use in my program. But I am not sure how to access the rest response status codes (in order to make my implementation more reliable). Also, I am not sure about what other flags should I provide to the cURL while calling it.
PS: Just to add, if you think there is some other MUCH better alternative to retrieving data from REST API other that using cURL, then also please let me know. Though my application is not web based and is more data crunching oriented and that's why I chose commandline and bash.
curl -i will print out the header information:
HTTP/1.1 200 OK
Date: Sat, 29 Dec 2012 07:34:00 GMT
I know it doesn't directly answer your question, but I'd suggest using Python instead of bash, for a number of reasons. And if you decide on that, I strongly recommend the marvellous Requests library: http://docs.python-requests.org

REST how to handle query parameters when put to resource?

I have a REST data service where I want to allow the users to create new items with HTTP PUT using different formats like json,xml,csv. I'm unsure how to best handle the format specification in the url:
PUT /ressource/ID/json
PUT /ressource/ID/xml
or
PUT /ressource/ID?format=json
PUT /ressource/ID?format=xml
So what is the best way to specify a format indicator?
If I specify the format with an query parameter and want to do a PUT how can I do this with curl?
curl -T test/data.json -d "format=json" http://localhost:5000/resource/33
does not work.
curl -T test/data.json http://localhost:5000/update?format=json
works, but I would rather let curl build the query parameters instead of adding them by myself.
A general principle of RESTful web services is to use the features built-in to HTTP, when applicable. In this case, you can indicate the format of your PUT request's content by setting the Content-Type header to application/json or application/xml.