Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point how to test an API adress by using directly curl command line ?

Hi Experts

Could you point how to test an API adress by using directly curl command line ?

As pointed here I could go to

 https://curl.haxx.se/

Open in new window


But I couldn't find the sequence that must be used after to use curl command line.

Thanks in advance
Avatar of David Favor
David Favor
Flag of United States of America image

How you do this will vary between APIs.

Most API docs list many curl tests/examples.

https://documentation.mailgun.com/en/latest/api-sending.html#examples provides one such example, describing how to send a message through the https://MailGun.com relay service using curl.

Note: The above syntax/examples only work with MailGun, so you'll refer to your docs for how to do something similar with your specific API.
Avatar of Eduardo Fuerte

ASKER

but before that, how to get the "curl" executable?

(Downloads are suggested on the site ... I downloaded tiny-curl.
But source code came in C language ...)
Hi


My personal PC is Win10  but I have to test it at a  "Windows Server 2012 R2" accessed by  IAP Desktop - Identity Aware Proxy for Remote Desktop (running at Google Cloud).
Then you just need to unzip the binary (should be an exe) according to the internet
In your php/ext folder, are you sure you don't already have a php_curl.dll file? If so, then you just need uncomment the line in your php.ini file for extension=curl

After that, you can use curl commands. I am on windows and find I have to add a line for my intermediary cert for things to work. You can pick it up https://curl.haxx.se/docs/caextract.html and using it similar to
curl.cainfo = "C:\path_to\cacert.pem"

Open in new window

 
1. Download package (I get mine from here) https://curl.se/windows/
2. Unzip to a folder
3. Add folder to path
4. Open command prompt and type curl -V - this should show you what version you are using.

If you get this far you are good to go with your cUrl testing.

Having said that - my personal preference for testing API's is PostMan

This is by far the most versatile and powerful tool for doing API testing and saves you having to type in long strings on the command line.

I second postman for testing.  One option I like about using postman is you can mostly import, export or translate code for just about any language.
As Scott mentioned, if you have problems with curl, use the https://postman.com/ API POST testing facility.
Just to make it clear.

I'm running on an unusual situation.

We have to call an API by using an old PHP(5.3) code.to do that.

Actually curl is not used to comunicate to the API at this code, we first need to use a PEAR code.

Probably the company is not going to accept the change to use curl..

The justification is we couldn't change hundreds of instalations using these configurations that doesn't use curl.

The test here by using curl is something to separate things.

I had configuerd  my own instalation to use curl few days before from PHP5.3 with no success at DEV environment, having a lot of help here... curl function was launched but couldn't find the API enndpoint with success.

By using this curl line command (outside PHP) on Windows Server 2012 R2, if ok, I get an evidence that the trouble we have is not with O.S.

After...
By using POSTMAN it runs OK....  so I guess we could overpass the test with CURL.

My conclusion is... the problem is with old PHP code...
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The use of cUrl in your code (PHP) has no bearing on whether you should use cUrl (command line) to test the API or Postman - hopefully that clears things up a bit.

Certainly.

In the meanwhile with some trial and errors someone find that doing that way:

http://apihub-qa.br.wcorp.xxxxx.com/mgus-coupon-active/query

Open in new window


It runs on DEV environment.
Thank you for all the replies!!!
1) We have to call an API by using an old PHP(5.3) code.to do that.

You're going to be in a world of hurt soon.

If you're running PHP-5.3, this also means you're running a very old version of OpenSSL too.

This means, if you actually own/control the API endpoint + can suppress updates, keeping PHP-5.3 will work.

If you don't own/control the API endpoint, eventually the API code will update + your version of OpenSSL will no longer communicate, so if this is the case... one day... likely very soon... all your 100s of installations will all fail simultaneously.

Fix: Use phpcs to upgrade all your PHP-5.3 source code, first to PHP-5.6 (easy), then eventually to PHP-7.4 (harder), then to PHP-8.0 (difficult).

2) Actually curl is not used to communicate to the API at this code, we first need to use a PEAR code.

If you're using PEAR, this suggests the API endpoint you're accessing is HTTP, rather than HTTPS.

If this is true, then both PHP-5.3 + your API endpoint are hackable.

Shudder...

3) Probably the company is not going to accept the change to use curl...

If the goal is preservation of current income, with no interruption, then upgrading PHP-5.3 -> PHP-7.4 + upgrade OpenSSL to latest, will take a high priority.

4) The justification is we couldn't change hundreds of installations using these configurations that doesn't use curl.

See #3. The question is. If income goes to $0, which it will eventually using old tech, then is it better to way till income goes to $0, then try to fix everything during the catastrophe or is it better to make changes now, with no rush.

5) The test here by using curl is something to separate things. I had configured  my own installation to use curl few days before from PHP5.3 with no success at DEV environment, having a lot of help here... curl function was launched but couldn't find the API endpoint with success.

This has nothing to do with curl + everything to do with dev setup.

When networking works, curl works.

6) By using this curl line command (outside PHP) on Windows Server 2012 R2, if ok, I get an evidence that the trouble we have is not with O.S.

Correct. The curl tool is a workhorse. Rarely will you find a version that somehow fails to work.

7) After...
By using POSTMAN it runs OK....  so I guess we could overpass the test with CURL.

My conclusion is... the problem is with old PHP code...

Yes + of greater issue is your OpenSSL version, if your API endpoint uses HTTPS, rather than HTTP.
Aside: https://www.experts-exchange.com/questions/29150594/Updating-website-page-to-PHP-7.html provides how most large projects fix their code, before updating PHP versions, so they know their code will survive the update.

Above I mention going this route - PHP-5.3 -> PHP-5.6 -> PHP-7.4 -> PHP-8.0 - because this is the easy path.

If you attempt skipping any of these steps, the upgrade process will be more difficult.

In the sequence above, once you do one incremental PHP source upgrade, you can run all the upgraded code on existing version.

For example, if you run phpcs + upgrade all your PHP-5.3 code to PHP-5.6, then you can usually run all your PHP-5.6 code under PHP-5.3, so you can make incremental changes/upgrades to source code, leaving system running on PHP-5.3 till all your PHP-5.6 source code changes are made, then upgrade to PHP-5.6.

Then follow this same approach going to higher levels of PHP.

This is the normal sequence most large projects use, when upgrading through many PHP versions.