Simplest REST API example

Hi there,

Iā€™ve just met Plotly and I think it is great tool which can be of importance in a company I am currently working in. So Iā€™ve decided to check out REST API - my probable use case would be creating data in internal application and then export it to Plotly in order for people to see it/edit/comment/etc.

So I am trying to create simplest ā€˜Hello worldā€™ example but I am stuck. I was trying to use simple HTML page with jQuery AJAX call to REST API but I stumbled upon CORS/cross-domain SSL issues. Ok, we can skip that. Iā€™ve switched to Fiddler for the sake of simplicity.

I am pasting simple JSON (taken from DOCS, with my login/pass) as request body but I am on and on reciving error message.

Here is the JSON request body taken from Fiddler:

{un: ā€˜chlebikā€™, key: ā€˜MY_API_KEY_TAKEN_FROM_SETTINGSā€™, origin: ā€˜plotā€™, platform: ā€˜lispā€™, args: [[0, 1, 2], [3, 4, 5], [1, 2, 3], [6, 6, 5]],
kwargs: {ā€œfilenameā€: ā€œplot from apiā€,
ā€œfileoptā€: ā€œoverwriteā€,
ā€œstyleā€: {
ā€œtypeā€: ā€œbarā€
},
ā€œtracesā€: [1],
ā€œlayoutā€: {
ā€œtitleā€: ā€œexperimental dataā€
},
ā€œworld_readableā€: true
}
}

Whatever I do I get ā€˜Missing required POST parameters: platform un key origin args kwargsā€™ despite the fact that body is recognised by Fiddler as JSON - event JSON-tree is created.

So the question is - am I so tired by now just to miss something obvious or is there some catch I have no idea about?

Hi,
I tested this using Python, and what worked for me was json-serializing the list for both the args and kwargs keys, but not the entire dictionary.

import requests
import json
payload = {
    'un': 'username',
    'key': 'api_key',
    'origin': 'plot',
    'platform': 'python',
    'args': json.dumps([[0, 1, 2], [3, 4, 5], [1, 2, 3], [6, 6, 5]]),
    'kwargs': json.dumps({"filename": "plot from api",
            "fileopt": "new",
            "style": {
            "type": "bar"
            },
    "traces": [1],
    "layout": {
        "title": "experimental data"
    },
    "world_readable": True
    }) 
}
r = requests.post('https://plot.ly/clientresp', data=payload)

My understanding is that https://plot.ly/clientresp is depreciated in favor of the API version 2: https://api.plot.ly/v2/ . Unfortunately, the new api is quite badly documented and I cannot figure out how to create a simple chart in a single http POST.

good luck.

didier