Sign In Register

How can we help you today?

Start a new topic
Answered

REST API JSON instead of form data

I'm using the REST API for some data automation stuff, and I've built it to send JSON formatted requests.


The REST API docs describe using form data for requests, and I can't find an example of what the same requests should look like in JSON. I think I got it right, but my calls are not resulting in any changes to the collections.


I am pretty confident that I have the URL parameters correct because I can use the same mechanism to successfully receive a response to a Mongo stats call, for example.


  1. Is there any example of what a request payload looks like as JSON instead of form data?
  2. Is there any place to find REST call log output so I can potentially diagnose why my calls are not working?

Here's an example of my request data for a Mongo update: 

{
    "query": {
        "_id": "keys_template"
    },
    "update": {
        "ownCountMax": 0,
        "tags": []
    },
    "upsert": true
}

 


Best Answer

I finally got it working. I had to add fields separately using WWForm.AddField(), like:

  

var form = new WWWForm();
form.AddField( "query", queryJson );
form.AddField( "update", updateJson );
form.AddField( "upsert", "true" );

  


Hi Jason,


It is possible to send a JSON doc through Form Data using the REST API.


The full payload of the request can be retrieved by using Spark.getData() on its own. This delivers the Raw Request payload. To inspect this payload you could do a Spark.getLog().debug(<payload>) right after this. This will log the request in the NoSQL Database for you to inspect. Simply open the NoSQL Browser and look at the "script.log" runtime collection.


If you find that your raw response has been stringified then you can use JSON.parse to get the correct JSON again.


I often use a service like "Postman" to test REST endpoint callbacks and almost always set my form data to "application/json".


Please let me know if you have any more questions.


-Pádraig 

Sorry if I was unclear. I meant I would like to send the request body as pure JSON, with no form data. I know that services like Amazon API Gateway allow form data or JSON and accept both. But maybe GameSparks doesn't allow non-form data to be sent via REST API?


I'll try an arrangement using form data instead and see what happens.

Also I should note that I'm doing this with the Unity SDK from C#, using the UnityWebRequest object POSTing with a WWWForm.


I'm seeing this error now:

{"responseCode":-1,"errorMessage":"Failed to convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type 'java.lang.String'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [java.lang.String]: no matching editors or conversion strategy found"}

 If I change the Content-Type to something like multipart/form-data, I get a 404 page not found. If I change it to text/plain or application/json, it says the "query" parameter is not found. It's failing this way in both Postman and my Unity app.


I'm sure there's some minute detail I've missed, but it's a lot of trial and error so far. Is there a working example code I could use as reference?

Here's what Unity is sending, using the default behavior of the UnityWebRequest class.

 

--iKTMFezFDSw9SSTaxLcNAWrVmKaWFrybxBGMDuoI
Content-Type: application/octet-stream
Content-disposition: form-data; name="query"; filename="query.dat"

{
    "_id": "keys_template"
}
--iKTMFezFDSw9SSTaxLcNAWrVmKaWFrybxBGMDuoI
Content-Type: application/octet-stream
Content-disposition: form-data; name="update"; filename="update.dat"

{
    "ownCountMax": 0,
    "tags": []
}
--iKTMFezFDSw9SSTaxLcNAWrVmKaWFrybxBGMDuoI--

 

A big part of the problem is that I don't know precisely what GameSparks is expecting to receive. Is that information anywhere in the documentation about the REST API?


The examples I've seen only show vague information and what appears to be JSON, like this:

Form Data - query : { “_id”: “my key” }, update : {“my data”:”new value”}

Answer

I finally got it working. I had to add fields separately using WWForm.AddField(), like:

  

var form = new WWWForm();
form.AddField( "query", queryJson );
form.AddField( "update", updateJson );
form.AddField( "upsert", "true" );

  

Thanks Jason, glad it worked out for you!


-Pádraig

You post this with Spark.getHttp("http://somehost").postJson(jsonForm);?

Dan, I was doing it from the client side. I haven't tried it from cloud code.

Login to post a comment