REST/HttpSocket/JSON

Hi

I am testing a RESTful web service. I have got the basic HTTP GET working using HttpSocket. I have two ‘problems’ that I expect are down to my lack of Xojo experience but I have scoured the web and not found obvious answers.

(1) For a HTTP POST HttpSocket.SetFormData wants a Dictionary as a parameter, my form data is a JSONItem - do I really have to copy element by element or how else should I be setting the form data for a HTTP POST? The manual shows the use of SetFormData

(2) How do I perform HTTP PUT and HTTP DELETE? Only GET and POST are obvious for the HttpSocket class.

That’s what I have ended up doing in the past. Someone else may know a better technique, in which case I hope they share it.

[quote]
(2) How do I perform HTTP PUT and HTTP DELETE? Only GET and POST are obvious for the HttpSocket class.[/quote]

Use the SendRequest method and pass the HTTP verb as a string (e.g. myHTTPSock.SendRequest("PUT", "example.com/test"))).

[quote=21497:@Andrew L.]That’s what I have ended up doing in the past. Someone else may know a better technique, in which case I hope they share it.
[/quote]

Sharing is caring!! :slight_smile:

I found out that: mysock.SetRequestContent(request.Params.ToString(), "application/json") (where Params is a JSONItem property in my code) works well.

Thanks for the PUT/DELETE example.

In the LR there’s an example to convert a Dictionary into a JSONItem.

[code]Dim d As New Dictionary
d.value(“Name”) = “John Doe”
d.value(“Age”) = 32
d.value(“Married”) = True
d.value(“Spouse”) = “Jane Doe”

dim j as new JSONItem
j = d

[/code]

[quote=21503:@Wayne Golding]In the LR there’s an example to convert a Dictionary into a JSONItem.

[code]Dim d As New Dictionary
d.value(“Name”) = “John Doe”
d.value(“Age”) = 32
d.value(“Married”) = True
d.value(“Spouse”) = “Jane Doe”

dim j as new JSONItem
j = d

[/code][/quote]

Thanks for the suggestion Wayne, I did see that but actually needed to go the other way around, from a JSONItem to a Dictionary if I wanted to use the SetFormData method but then I realised that SetFormData may not be ‘smart enough’ to be able to set the MIME type properly and that I was being unreasonable to expect it to be able to so that.

It’s not smart enough, in fact it only supports application/x-www-form-urlencoded form data, such as a simple HTML form might produce, e.g.:

Name=John%20Doe&email=jdoe%40example.com&comment=No%20comment.

For arbitrary data (like serialized JSON,) you’ll have to roll your own with SetRequestContent, like you’re already doing.