Restful API

I need to make a call to a REST endpoint.

It works with Postman and this is an example of curl code generated by Postman:

curl --location ‘https://example.com/xxx
–header ‘X-Token: wic3ViIjoibnBhcHBhbGFyZG8iLC’
–header ‘Authorization: basic 4QUZGQTRBMDk5RTRFNkY=’ \
–form ‘payloadSend=“{ "field1":"field1", "field2":null,"field3":False,
"field4":{"field5":"Field5","field6":null},
}”;type=application/json’

In the Headers section of Postman I see:

KEY: Content-Type VALUE: multipart/form-data; boundary=calculated when request is sent

I have made many attempts with CURLSMBS and with URLConnection.

for URLConnection, I also used GitHub - einhugur/MultipartFormDataContent: MultipartFormDataContent class for Xojo

however, the call always ends with errors (status 400, 415 or 500 depending on the changes to the code).

I have seen that there are several discussions here in the forum and also elsewhere.

I don’t know if my case is different or if I still haven’t figured out how to act to successfully end the call.

I ask if anyone here can help me.

Thank you.

Hi Giacomo,

This is my interpretation of your request.

Var Data As New Dictionary
Data.Value("field1") = "field1"
Data.Value("field2") = Nil
Data.Value("field3") = False
Var Field4 As New Dictionary
Field4.Value("field5") = "field5"
Field4.Value("field6") = "field6"
Data.Value("field4") = Field4

Var Connection As New URLConnection
Connection.RequestHeader("X-Token") = "wic3ViIjoibnBhcHBhbGFyZG8iLC"
Connection.RequestHeader("Authorization") = "basic 4QUZGQTRBMDk5RTRFNkY="
Connection.SetRequestContent(GenerateJSON(Data), "application/json")

Var Result As String = Connection.SendSync("GET", "https://example.com/xxx")

I’ve used a dictionary to create your JSON data just because I’m comfortable with that. You could use JSONItem as an alternative.

2 Likes

thank you Wayne.

Unfortunately, I have made no progress.

here is my anonymized code:

Var Data As New Dictionary
Data.Value("field1") = "value1"
Data.Value("field2") = Nil
Data.Value("field3") = Nil
Data.Value("field4") = Nil
Data.Value("field5") = Nil
Data.Value("field6") = Nil
Data.Value("field7") = Nil
Data.Value("field8") = Nil
Data.Value("field9") = False
Data.Value("field10") = False
Data.Value("field11") = Nil
Data.Value("field12") = True

Var field13 As New Dictionary
field13.Value("field13s1") = "value2"
field13.Value("field13s2") = Nil
field13.Value("field13s3") = Nil
field13.Value("field13s4") = Nil
Data.Value("field13") = field13

Var field14 As New Dictionary
field14.Value("field14s1") = "value3"
field14.Value("field14s2") = "value4"
field14.Value("field14s3") = Nil
field14.Value("field14s4") = "value5"
field14.Value("field14s5") = "value6
field14.Value("field14s6") = ""
field14.Value("field14s7") = Nil
field14.Value("field14s8") = Nil
field14.Value("field14s9") = Nil
field14.Value("field14s10") = Nil
field14.Value("field14s11") = "value7"
field14.Value("field14s12") = "value8"
Data.Value("field14") = field14

Var Connection As New URLConnection
Connection.RequestHeader("X-Token") = "pbGUiOiI1Iiwic3ViIjoibnBhcHBhbGFyZG8iL..."
Connection.RequestHeader("Authorization") = "Basic QUZGQTRBMDk5RTRFNkY5Mzg1QTVFM..."
Connection.SetRequestContent(GenerateJSON(Data), "application/json")

Var sResponse As String = Connection.SendSync("POST", "https://www.example.com/...")
Var sStatus As String = Connection.HTTPStatusCode.ToString

the response is: “status”:415, “error”:“Unsupported Media Type”

I still hope for your help.
thank you again.

Can you provide a link to the API documentation?

Wayne’s code won’t work because the body of the request is form data, not raw JSON. That’s what --form does. In addition, --form also sets the request content-type to multipart/form-data.

The last part of the instruction in the form data field is supposed to set the Request Content header, which contradicts what you’re seeing from the “Postman headers section”. (I use Paw/RapidAPI so I do not know how this applies to the outbound request.)

Happy to help, but not going to guess. Please link the documentation.

(You can look up those HTTP error codes at https://http.cat to determine what they mean)

1 Like

I understand your point of view.
But I just verified that the documentation is not public, so I cannot provide it here.
I also thank you for the support and I hope to find a solution, also because of this latest suggestions.
Thank you all.

Well if you can clarify what type of Request-Content the API is actually expecting, we can try to build something from what we’ve got :slight_smile:

If the docs themselves offer CURL examples, those would be of use. It’s the output from Postman that’s confusing the problem (for me).

1 Like

thank you, Tim.

the CURL syntax from the docs:

curl -H "Content-Type: multipart/form-data" -H "X-Token: <token>" -F payloadSend="{ \"alias\": \"<alias>\", \"cName\": \"<cName> \", \"startDate\": null, \"endDate\": null, \"period\": null, \"recurrencesNumber\": null, \"recurrencesInterval\": null, \"validityPeriod\": null, \"sentNotify\": false, \"deliveredNotify\": false, \"msisdnToNotify\": null,\"sr\": false, \"cChannel\": { \"channelName\": \"UCP\", \"zone\": null, \"zoneType\": null, \"collectionTime\": null }, \"cMessage\": { \"messageType\": null, \"messageContent\": \"<messageContent>\", \"template\": null, \"file\": \"string\", \"msisdn\": \"<msisdn>\", \"listMsisdn\": null, \"fileMsisdn\": \"null\", \"name\": null, \"surname\": null, \"extra\": null, \"status\": \"string\", \"delivered\": \"string\" } };type=application/json" "https://example.com?destination=msisdn&message=static&delivery=immediate&type=single"

Method: POST

That’s super helpful and I’m working on something for you. The problem is that the MultipartFormData class is going to need updating, so I want to collaborate with Björn on the best way to do so. Is this okay or are you in need of a rush answer?

1 Like

Thank you Tim, any help will be appreciated. I hope soon, but I can’t ask for more.
I believe I understand that the multipart/form-data issue has been discussed many times, here in the forum and elsewhere.
If updating the MultipartFormData class will solve the problem permanently, I think it will be helpful for so many of us.

Does the Documentation shows a Javascript example? If so, can you post it?
Most modern APIs take JSON and sometimes it is not documented.
Just wondering.

The class is missing form field Content-Type headers, which I learned about today. I would guess from it not coming up previously that they aren’t used very frequently.

Updating it has been pretty straightforward, and with the nature of GitHub and open source I can post my changes and then Björn can decide whether or not to merge them into his official repository when he’s available.

Okay so here it is.

You will need my version of the MultipartFormData class (until the functionality gets merged) which you can grab from my GitHub but it’s also in the project below.

I prefer to maintain JSONItems in Xojo code rather than manipulating strings, so I built the JSON body with Xojo code. This ended up being long, so for clarity I put it in a function.

Give this a try and let us know how it goes. Without access to the API I am unable to test, but from everything I’ve gathered today I think this should work.

File: restful.xojo_xml_project

1 Like

Hi Mario,
no Javascript, only Php, if it’s of any interest to you.

Hi Tim,
it works!
I hope this will be helpful to those who will be in a similar situation, with limited API skills etc.

I thank you very much because I solved my problem.

it only remains for me to handle Send (instead of SendSync), even because first I have to get the token with a different call to the same service.
all within a webapp.

Glad to have helped!

You may want to explore building a class to handle communication with the API. In the past reusing a URLConnection could end up problematic, so as a safe practice I recommend a new URLConnection for each request.

I do spend a lot of time building solutions that work with remote REST APIs. If you are interested in having someone build out the API communication so you can focus on building your app, that is one of the services I can provide.

Hi,
now I saw that transferring to macOS the Xojo project (perfectly working on Windows with the MultipartFormDataContent class modified), the call to the https endpoint fails with status 415 (Unsupported Media Type).
any idea why the error occurs?
thank you.

Put a breakpoint on uc.SetRequestContent(formText, "") and take a look at the value of formText on both Mac and Windows. Compare the differences and correct the code to operate as expected.

1 Like

Thank you Tim.

Everything looks the same to me, except CRLF (looking at the hexadecimal content).
I tried LF, but nothing changed.
I will try again later, now I am satisfied that it works on Windows.
It is already a big step forward.

P.S. I also imported and ran with RapidApi (on Mac) the Postman project that works well on Windows.
Again I get error 415 (Unsupported media type).
So I think in general the conversion Win->Mac needs something I don’t know at this moment.