Magazines, Books and Articles

Showing posts with label Note to Self. Show all posts
Showing posts with label Note to Self. Show all posts

Thursday, January 22, 2015

Error using cURL to POST or PUT data formatted in JSON in Windows

If you use cURL in a cmd.exe shell on Windows, an attempt to POST or PUT data formatted in JSON results in an error. An example:

This is the data:
{"Continent":"AS","ContinentName":"Asia","CountryName":"India","Capital":"New Delhi","Iso_Alpha2":"IN","Iso_Numeric":"356","Iso_Alpha3":"IND","FipsCode":"IN"}
curl -i -X PUT -d "{"Continent":"AS","ContinentName":"Asia","CountryName":"India","Capital":"New Delhi","Iso_Alpha2":"IN","Iso_Numeric":"356","Iso_Alpha3":"IND","FipsCode":"IN"}"  -H "Content-type:application/json; charset=UTF-8" http://192.168.1.6:5984/geodb-country-test/IN

HTTP/1.1 400 Bad Request
Server: CouchDB/1.6.1 (Erlang OTP/R16B02)
Date: Thu, 22 Jan 2015 06:28:26 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 48
Cache-Control: must-revalidate

{"error":"bad_request","reason":"invalid_json"}
This is because the cmd.exe shell on Windows has issues parsing the quotes in the data.

One work around is to escape all quotes in the JSON. The following works:
curl -i -X PUT -d "{\"Continent\":\"AS\",\"ContinentName\":\"Asia\",\"CountryName\":\"India\",\"Capital\":\"New Delhi\",\"Iso_Alpha2\":\"IN\",\"Iso_Numeric\":\"356\",\"Iso_Alpha3\":\"IND\",\"FipsCode\":\"IN\"}"  -H "Content-type:application/json; charset=UTF-8" http://192.168.1.6:5984/geodb-country-test/IN

HTTP/1.1 201 Created
Server: CouchDB/1.6.1 (Erlang OTP/R16B02)
Location: http://192.168.1.6:5984/geodb-country-test/IN
ETag: "1-d4c8b330d781b982184e0e6829f434cd"
Date: Thu, 22 Jan 2015 06:31:26 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 65
Cache-Control: must-revalidate

{"ok":true,"id":"IN","rev":"1-d4c8b330d781b982184e0e6829f434cd"}
Escaping all the quotes in the data would be a huge pain; here's an online tool that will do all the donkey's work.

Another work around is to write the data to a file and use it in the following manner:
curl -i -X PUT -d @"C:/Shared Folder/country.json"  -H "Content-type:application/json; charset=UTF-8" http://192.168.1.6:5984/geodb-country-test/countries

HTTP/1.1 201 Created
Server: CouchDB/1.6.1 (Erlang OTP/R16B02)
Location: http://192.168.1.6:5984/geodb-country-test/countries
ETag: "1-568f829b1bcc952ba27ca7a084428390"
Date: Thu, 22 Jan 2015 07:42:12 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 72
Cache-Control: must-revalidate

{"ok":true,"id":"countries","rev":"1-568f829b1bcc952ba27ca7a084428390"}
cURL
Windows Installer

Friday, July 18, 2008

Copy database diagrams in SQL Server 2005/2008/2012

To copy database diagrams from database dbA to database dbB, run the following in the Query Analyser:

INSERT INTO dbB.dbo.sysdiagrams
SELECT[name],[principal_id],[version],[definition]
FROM dbA.dbo.sysdiagrams

--SELECT * FROM dbB.dbo.sysdiagrams


[Thanks to Pankaj Saha, a colleague at work, for this code snippet.]

Edit: This also works for SQL Server 2008.

Edit: This also works for SQL Server 2012, Service Pack 4.

Edit: Some other links that could be useful:
http://stackoverflow.com/questions/3310137/sql-server-2005-how-to-copy-a-database-diagram-to-another-server
If you get an error "Cannot insert the value null into column "diagram_id"", check this out: http://social.msdn.microsoft.com/Forums/en-US/sqldocumentation/thread/767de035-5509-4150-af21-8b6752653f05