Sign In Register

How can we help you today?

Start a new topic

Trouble POSTing using NoSQL REST API

I've figured out how to GET data using the NoSQL REST API, but all of my attempts to POST are failing.


I'm using Python with the requests library. Sample code follows:



 

import requests
import json

# ... authentication code removed for brevity

headers = {
    'X-GS-JWT': auth_info['X-GS-JWT'],
    'Content-type': 'application/json; charset=UTF-8'
}

collection = "player"

#url = "%s/restv2/game/%s/mongo/collection/%s/stats" % (endpoint, apikey, collection)
url = "%s/restv2/game/%s/mongo/collection/%s/find" % (endpoint, apikey, collection)

payload = {"fields": {}, "limit": 10, "query": {}, "skip": 0, "sort": {}}

#r  = requests.get(url, headers=headers)
r = requests.post(url, data={"dto": payload}, headers=headers)

print (r.text)

r.raise_for_status()

 

When I comment switch the commented lines to GET the stats, it works. When I try to do the POST (as-is), it doesn't work. I've tried several variations -- I doubt the 'dto': payload bit is right, but I don't know what the dto field mentioned in the docs is talking about.


Thanks!



 I forgot to mention, here is the error I get:


The API replies with the text: {"errorCode":"bad_request"}

and I get an HTTP 400 error code.

Hey, I got it. The solution is to change one line.

 

r = requests.post(url, data=json.dumps(payload), headers=headers)

I just needed to change a python object to a json string.


Hat tip to the last answer at https://support.gamesparks.net/support/discussions/topics/1000080902


Login to post a comment