Had to fiddle about a bit to work out how to POST an JSONAPI with Unity via C#, this example sends raw JSON with the application/json header which Rails will then decode it and turn it into ‘params’.
using UnityEngine;
using System.Collections;
public class ThingAPI : MonoBehaviour {
// Use this for initialization
IEnumerator Start () {
// GET
var url = "http://localhost:3000/api/posts/1";
WWW www = new WWW(url);
yield return www;
// POST
url = "http://localhost:3000/api/posts";
var jsonString = "{\"post\":[{\"title": "Something to post about\": 1, \"body\": \"and maybe someone will listen to my cries.\"}]}";
var encoding = new System.Text.UTF8Encoding();
var postHeader = new Hashtable();
postHeader.Add("Content-Type", "application/json");
postHeader.Add("Content-Length", jsonString.Length);
www = new WWW(url, encoding.GetBytes(jsonString), postHeader);
yield return www;
}
}
I, [2015-01-22T07:15:59.225481 #30041] INFO -- : Rendered api/characters/_character.json.jbuilder (0.0ms)
I, [2015-01-22T07:15:59.225700 #30041] INFO -- : Rendered api/characters/_character.json.jbuilder (0.0ms)
I, [2015-01-22T07:15:59.225951 #30041] INFO -- : Rendered api/characters/_character.json.jbuilder (0.1ms)
I, [2015-01-22T07:16:00.561068 #30041] INFO -- : Rendered api/characters/_character.json.jbuilder (1307.7ms)
I, [2015-01-22T07:16:00.561527 #30041] INFO -- : Rendered api/characters/_character.json.jbuilder (0.1ms)
I, [2015-01-22T07:16:00.561772 #30041] INFO -- : Rendered api/characters/_character.json.jbuilder (0.1ms)
I, [2015-01-22T07:16:00.561993 #30041] INFO -- : Rendered api/characters/_character.json.jbuilder (0.1ms)
I, [2015-01-22T07:16:00.562210 #30041] INFO -- : Rendered api/characters/_character.json.jbuilder (0.0ms)
I, [2015-01-22T07:16:00.562425 #30041] INFO -- : Rendered api/characters/_character.json.jbuilder (0.1ms)
I totally managed to mess up my Gemfile.lock file setting the versions of many gems to far higher versions than I’d intended. bundle update is bad, even if you set the version in your Gemfile the dependencies of thos gems are not versioned so precisely. I did this in a branch so fortunately the Gemfile.lock in my master branch is accurate.
git checkout master -- Gemfile.lock
That did the job. Then a bundle install puts the changes I’d made to my Gemfile into Gemfile.lock with the correct dependencies as per master. Phew.
Robs-iMac:~ rl$ brew install
/usr/local/bin/brew: /usr/local/Library/brew.rb: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby: bad interpreter: No such file or directory
/usr/local/bin/brew: line 26: /usr/local/Library/brew.rb: Undefined error: 0
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
/Users/rl/.rvm/rubies/ruby-2.1.2/bin/ruby extconf.rb
creating Makefile
make "DESTDIR=" clean
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
make "DESTDIR="
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
make failed, exit code 69
Gem files will remain installed in /Users/rl/.rvm/gems/ruby-2.1.2@upgrade-ruby/gems/byebug-3.5.1 for inspection.
Results logged to /Users/rl/.rvm/gems/ruby-2.1.2@upgrade-ruby/extensions/x86_64-darwin-13/2.1.0-static/byebug-3.5.1/gem_make.out
An error occurred while installing byebug (3.5.1), and Bundler cannot continue.
Make sure that `gem install byebug -v '3.5.1'` succeeds before bundling.
You need to do sudo xcrun cc
..blah blah blah...
By typing 'agree' you are agreeing to the terms of the software license agreements. Type 'print' to print them or anything else to cancel, [agree, print, cancel] agree