user156888
user156888

Reputation:

Url Encoding an array

This might seem dirty but it's for documentation purposes I swear!

I am accessing my services using GETs in my documentation so people can try things out without needing to get too complicated.

Appending x-http-method-override=POST to the URL forces the server to take a GET as a POST

This is all good except when I need to POST an array of objects. This would be simple in a standard POST but today I have a new bread of nightmare.

The expected POST looks like:

{"name":"String","slug":"String","start":"String","end":"String","id":"String","artists":[{"id":"String","name":"String","slug":"String"}],"locationId":"String"}

As you can see there is an array of artists up in here.

I have tried to do the following:

model/listing?start=10:10&end=12:30&artists[0].name=wayne&artists[0].id=artists-289&locationid=locations-641&x-http-method-override=POST

But to no avail.

How can I get an array of objects into a URL so that service stack will be happy with it?!

I appreciate this is not the done thing but it's making explaining my end points infinitely easier with clickable example URLs

Upvotes: 1

Views: 794

Answers (1)

Mike Mertsock
Mike Mertsock

Reputation: 12015

You can use JSV to encode complex objects in the URL. This should work for your DTO:

model/listing?name=wayne&artists=[{id:artists-289,name:sample1},{id:artists-290,name:sample2}]&locationId=locations-641

You can programmatically create JSV from an arbitrary object using the ToJsv extension method in ServiceStack.Text.

Upvotes: 2

Related Questions