Curl and urlconnection

I am trying to connect to google healthcare cloud with an API key. They say I should make a Curl request like this:
curl -X POST
-H “Authorization: Bearer $(gcloud auth print-access-token)”
-H “X-goog-api-key: API_KEY”
-H “Content-Type: application/json; charset=utf-8”
-d @request.json
https://translation.googleapis.com/language/translate/v2

Can this be done with URLConnection?

Yes.

Update: Now that I’m at a computer,

var oSock as new URLConnection
oSock.RequestHeader("Authorization") = "Bearer {Your gcloud auth access-token}"
oSock.RequestHeader("X-goog-api-key") = "API_KEY"
oSock.RequestHeader("Content-Type") = "application/json; charset=utf-8"

// @request.json means to use the file request.json
// I'm going to assume you know how to read a file in Xojo
// If you are building a JSONItem in Xojo code, you do NOT have to write it to a file
oSock.SetRequestContent("The request.json file contents")

oSock.Send("POST", "https://translation.googleapis.com/language/translate/v2")
1 Like

Thanks. I guess this could work but I really don’t have a clue what “request.json” is. I only have the API_KEY they made me generate.

You build the request for the data you need using the documentation. That’s not something related to CURL or URLConnection.

How about adding a file with additional field information?

In some API’s documentation I read:

-H "Authorization: Bearer {your api key}"
-H "Content-Type: multipart/form-data"
-F file="{path to audio file}"
-F model="{desired model}"

For multipart/form-data you can use the open source class from Einhugur:

It supports attaching a FolderItem :slight_smile:

1 Like

Thank you so much, Tim!

I checked the code. And I noticed that the file is provided in pure binary format.

I’m not really familiar with this kind of stuff.
Should it be some kind of Base64 encoded data? Or does it not really matter?

No, multipart is binary data.

2 Likes

Should be:

oSock.SetRequestContent("The request.json file contents", "application/json; charset=utf-8")

Ah thanks for the correction!