Convert API call to Xojo

I’m trying to create emails with the API of Selzy. I can use Postman fine:

A simple GET call copied into the browser works fine:

https://api.selzy.com/en/api/createEmailMessage?format=json&api_key=65ohxxxjy&sender_name=Beatrix&sender_email=mail@beatrixwillius.de&subject=SUBJECT&body=HTMLBODY&list_id=1

When try to use a UrlConnection I only get access denied:

Dim FormData As New JSONItem
FormData.Value("api_key") = "65oh666xxx"
FormData.Value("sender_name") = "Beatrix Willius from Moth Software"
FormData.Value("sender_email") = "mail@beatrixwillius.de"
FormData.Value("subject") = "test subject"
FormData.Value("body") = "<html><body>test body</body></html>"
FormData.Value("list_id") = "1"

theSocket.SetRequestContent(FormData.ToString, "application/json")
dim theResult as String = theSocket.SendSync("POST", "https://api.selzy.com/en/api/createEmailMessage")

dim ResultDictionary as Dictionary = ParseJSON(theResult)
if ResultDictionary.HasKey("error") then
  MessageBox("There was an error when trying to create an email: " + EndOfLine + EndOfLine + ResultDictionary.Value("error"))
  Return
end if

What am I doing wrong?

What I think the documentation is suggesting for POST is to send form data. You can do this with the free open source class from Einhugur

Let us know if you get stuck or it doesn’t work.

Edit: Here’s all I could find about GET / POST and format

1 Like

Thanks, I’ll try that.

Selzy’s API is a bit strange, it looks like everything related to the email should be included in the URL parameters. Other APIs use POST data.

Selzy is expecting everything in the URL parameters, just like you did in Postman.

https://api.selzy.com/en/api/sendEmail?format=json&api_key=KEY &email
=TONAME <EMAILTO>&sender_name=
FROMNAME&sender_email=FROMMAIL&subject=SUBJECT
&body=HTMLBODY&list_id=X&attachments[filename1]=FILE1&attachments
[filename2]=FILE2&lang=LANG&error_checking=1&metadata[meta1]=
value1&metadata[meta2]=value2

Are you sure the server is expecting a json?

Why not send it all the params in the URL like with Postman?

The “format” param you send in Postman is to tell the server the format of the response you are epecting.

Also, it looks like Selzy is for sending email campaigns, as in sending one or more emails to a group of people.
Is that really what you need @Beatrix_Willius ?

If you are looking for transactional emails (as in sending a different email to each person) it looks like you will need to use https://unione.io which is owned or a partner of Selzy.

@Jeremie_L : Selzy also allows POST data which is I’m trying to get working. GET works fine. I’m trying to send bulk emails to a list. I want to get rid of MailChimp where I still use RSS to email.

@Ivan_Tellez : No, I’m not sure if this is needed here.

@Tim_Parnell : I get the same result as before:

Dim FormData as MultipartFormDataContent = new MultipartFormDataContent()
FormData.Add("api_key", "65oh666cxxx")
FormData.Add("sender_name", "Beatrix Willius from Moth Software")
FormData.Add("sender_email", "mail@beatrixwillius.de")
FormData.Add("subject", "test subject")
FormData.Add("body", "<html><body>test body</body></html>")
FormData.Add("list_id", "1")
FormData.Add("format", "json")
FormData.SetURLConnectionMultipartContent(theSocket)

Dim result as String = theSocket.SendSync("POST", "https://api.selzy.com/en/api/createEmailMessage")

Do you have a link to the documentation that talks about POST? I couldn’t find it, but reading it might help understand the exact format.

See my post above, it’s all I could find about the optional POST method.

Can you show the raw request Postman sends? I know Paw/RapidAPI can show that, it would be helpful and could illustrate the difference in requests. You’re welcome to send it privately if necessary.

All parameters should be encoded in UTF-8. In the example above the parameters are given in the GET request, but you can send them through POST as well. Moreover, we strongly recommend that you send the api_key parameter through POST to ensure it’s not saved in proxy servers logs.

Here is the raw command from Postman:

POST https://api.selzy.com/en/api/createEmailMessage?api_key=65oh666xxx&sender_name=Beatrix%20Willius%20from%20Moth%20Software&sender_email=mail@beatrixwillius.de&subject=test%20subject&body=%3Chtml%3E%3Cbody%3Etest%20body%3C/body%3E%3C/html%3E&list_id=1&format=json

I don’t think that this is form data.

Yea if it was Formdata then your parameters in your Postman would be in the body section like this:

So, yes it is not formdata. And More like just parameters, like Jeremie was hinting I think.

1 Like

You are correct, that looks like GET parameters to me…

Ah, I don’t use Postman so I wasn’t sure where those items ended up. I use Paw / RapidAPI :person_shrugging:

1 Like

From their docs:

The list of parameters for the method called is transferred in params . Parameters can be transferred either via a GET request or via POST. You are strongly recommended to use only POST requests, since the data transferred in them is not stored in logs of the servers engaged in the connection. This increases the security of your work through API.

So you could use “x-www-form_urlencoded” (or even “mutlipart/form-data”)

Something like:

var params As string = "param1=value1&param2=value2&param3=value3"
// you may be required to EncodeURLComponent all of the values.

mySocket.SetRequestContent(params, "x-www-form_urlencoded")
mySocket.Send("POST", "https://api.selzy.com/en/api/createEmailMessage")
1 Like

Thanks for your help, guys.