0

I'm running a cURL shell script from an (indesign) extendscript jsx-file.

This works well for simple queries, but in the command below you can see that I'm having quite some quotation nesting, which seem to be making the command fail! It's not throwing any errors, but the Wordpress-Post is not being changed!

var command = 'do shell script "curl -X PUT -H \'Content-Type: application/json\' -d \' { \'title\': \'Blub\', \'content_raw\': \'Updated post content\' } \' -u username:password http://website.com/wp-json/wp/v2/cpt/12/"';
var response = app.doScript(command, ScriptLanguage.APPLESCRIPT_LANGUAGE);

When I write it like this it throws an error, because of false quotations:

var command = 'do shell script "curl -X PUT -H \"Content-Type: application/json\" -d \" { \"title\": \"Blub\", \"content_raw\": \"Updated post content\" } \" -u admin:password http://seebook.dev/wp-json/wp/v2/calendar/12/"';

When I run the command directly in the terminal, it works...

curl -X PUT -H "Content-Type: application/json" -d " { \"title\": \"Updates Title\", \"content_raw\": \"Updated post content\" } " -u username:password http://website.com/wp-json/wp/v2/cpt/12/

So does this version:

var command = 'do shell script "curl -X PUT -H \"Content-Type: application/json\" -d \" { \'title\': \'Blub\', \'content_raw\': \'Updated post content\' } \" -u username:password http://website.com/wp-json/wp/v2/cpt/12/"';

How can I write the query, so that it works?

I guess that's quite a noob question, but I've never had such a situation before...

All the best, Lukas

der-lukas
  • 936
  • 9
  • 13
  • 1
    Just a thought... why are you doing it as Applescript? You surely don't need to start Applescript to start a shell command - can't you start `curl` directly? Or start `bash` directly without the extra layer of Applescript? – Mark Setchell Jan 16 '16 at 23:22

1 Answers1

0

Try this, the outer quotes must be double quotes, the content-type part is enclosed in single quotes and the query is created with the quoted form of expression

set query to "{ \"title\": \"Updates Title\", \"content_raw\": \"Updated post content\" }"
do shell script "curl -X PUT -H 'Content-Type: application/json' -d " & quoted form of query & " -u username:password http://website.com/wp-json/wp/v2/cpt/12/"
vadian
  • 274,689
  • 30
  • 353
  • 361