Help with curl notation for Web API call

I’m trying to wrap my ahead around curl notation… I know this is not exactly a Thunkable question but it’s about to become one when I create the blocks for this…

I’m trying to get info from Microsoft’s AI Earth Web API. The documentation shows this Request URL and sample curl command but it doesn’t include the image url that I need to send along with the command. So I’m trying to add that in.

Request URL
https://aiforearth.azure-api.net/species-classification/v2.0/predict[?topK][&predictMode]

@ECHO OFF

curl -v -X POST "https://aiforearth.azure-api.net/species-classification/v2.0/predict?topK={integer}&predictMode={string}"
-H "Ocp-Apim-Subscription-Key: {subscription key}"

--data-ascii "{body}" 

This is what I have so far. Depending on how I tweak the command, I can get various responses that indicate a failed send or a misformatted media format. I’ve tried about 20 different combinations so I’m hoping someone more familiar with curl can assist me. Note that the Subscription Key shown here is not a valid one; I do have a valid key.

curl -v -X POST -H “Ocp-Apim-Subscription-Key: lOEflwP0ouh9tv6zmKr” -H “Content-Type: application/json” -d “{url: http://2.bp.blogspot.com/-_fWU03Q9sDM/Ubwtc_ipPII/AAAAAAAAAfk/GnUQG6t5rto/s1600/Barn-Owl-Facts.JPG}” "https://aiforearth.azure-api.net/species-recognition/v2.0/predict?topK=1&predictMode=classifyOnly”

And here are the parts of that same command split apart for easier viewing:

curl -v -X POST

-H “Ocp-Apim-Subscription-Key: lOEflwP0ouh9tv6zmKr”

-H “Content-Type: application/json”

-d “{url: http://2.bp.blogspot.com/-

_fWU03Q9sDM/Ubwtc_ipPII/AAAAAAAAAfk/GnUQG6t5rto/s1600/Barn-Owl-Facts.JPG}”

"https://aiforearth.azure-api.net/species-recognition/v2.0/predict?topK=1&predictMode=classifyOnly”

Have you tried blocking it out with the WEB_API? I know a few tricks for translating CURL into blocks, but I don’t know how to work directly with CURL

@drted Yeah, I started with blocks, actually. And then when it became tedious to keep changing them, I switched to curl. Also, the Web_API returns only a status value from their servers. Nothing’s ever in the green error block. Whereas when I run it in Terminal using curl, I at least get some error messages back from their server.

I think I just have some syntax wrong. Probably a header or parameter name. I’m checking with the director of the program so hopefully he will set me straight.

I’m honestly not always sure what goes in url vs. header vs. query parameter blocks. I end up doing a lot of trial and error until I hit upon the right combination.

@drted

Screen Shot 2020-11-17 at 11.36.34 AM

@tatiang,

A few thoughts:

I’ve found APIs put info in the darnedest places, so I always display error, status, and response when debugging:
image

It looks like you need 2 header properties:

curl -v -X POST -H “Ocp-Apim-Subscription-Key: lOEflwP0ouh9tv6zmKr” -H “Content-Type: application/json”

Don’t be like me and try to put them in a list. It should be one object with 2 properties.
I think the url should be in the body, not the header ( but I might be wrong).

When using the body, you cannot use Thunkable object blocks directly, you need to GENERATE JSON FROM OBJECT (because Thunkable)
image

I’ve struggled to get the parmeter block to work consistently. Here is an example of it working

Unfortunately, sometimes I resort to JOINing a long URL block :frowning:. Honestly, I usually start here, and once I get it to work, I move them to blocks, because Thunkable.
image

Happy Thunking!

Thanks, @drted. I take the same approach. I actually was displaying the error, status and response above… I just assumed that a response wouldn’t be helpful if there was an error or the status was ≠ 200. But I’ll try your way.

Edit: you’re right! It was returning a response along with a status ≠ 200.

I like this little trick:

And yeah, I too start with a long url that contains everything and then break it apart with join blocks once it’s working. I just couldn’t get to that point.

Here’s what I have so far. I think I might be able to parse the right fields from this document that I just found: https://aiforearth.portal.azure-api.net/docs/services/species-classification-v2/export?DocumentFormat=OpenApiJson. Especially after running it through this for better formatting: https://jsonformatter-online.com.

These blocks


Give me this result

Are you able to connect without the body?
Do you have the same URL as I do (yours has elipses)?

Yep, same url as you: https://aiforearth.azure-api.net/species-classification/v2.0/predict

I’m also trying Content-Type: application/octet-stream

Yeah, that documentation assumes more than I know. Sorry, I’m stumped :frowning:

That’s okay, I appreciate you trying!

I think I understood most of this. But why are you using if status is not equal to 200? What exactly does ‘200’ signify?

Thanks @drted for trying this out!

JSON responses return a “status” value. If it’s 200, it indicates that the JSON call/request went through correctly. You can see a description of that here (as well as many other sites): https://restfulapi.net/http-status-codes/#:~:text=200%20(OK),should%20include%20a%20response%20body.

1 Like

Hm… ok.

Thanks!