Posting with cURL withOUT the command line?

I don’t “speak” AppleSriptObjC, in ObjC you can post data with this URL request sample code.
Of course you must adjust the HTTPHeaderField parameter

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSString *post = @"post this string": NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody];

That really helps. I couldn’t find anything about that in Google or that documentation or on MacScripter. I’ll convert it when I have time. That saves time~ lots of it.

Errors… and the first one I can’t understand. :confused:

I have:

on receiveaction_(sender)
		set thetweet to tweetfield's stringValue() as string
		if (thetweet's length) ≤ 140 then
			set sendpass to thepassword's stringValue()
			set senduser to theusername's stringValue()
			tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
			tell current application's class "NSMutableURLRequest" to set therequest to its requestWithURL_cachePolicy_timeoutInterval_(theurl, class "NSURLRequestUseProtocolCachePolicy", 60.0)
			tell current application's class "NSData" to set postdata to thetweet's dataUsingEncodeing_allowLossyConversion_(class "NSASCIIStringEncodeing", false)
			tell current application's class "NSString" to set postLength to (postdata's length)'s stringWithFormat_("%d")
			request's setHTTPMethod_("POST")
			request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
			request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
			request's setHTTPBody_(postdata)
		end if
	end receiveaction_

I’ve been working on it for a while; days; and nothing. :frowning:

When that error says argument 3, it means argument 2 – I suspect someone got confused with zero-based counting. So it probably means that:

class “NSURLRequestUseProtocolCachePolicy”

should be:

curent application’s NSURLRequestUseProtocolCachePolicy

It’s an enumeration, not a class.

Constants like “NSURLRequestUseProtocolCachePolicy” don’t work in ASOC (at least they don’t for me). Try replacing “class “NSURLRequestUseProtocolCachePolicy”” with 0.

Also, I couldn’t find “dataUsingEncodeing_allowLossyConversion” in the NSData reference – it is in NSString ( also there is no “e” after the “d” in Encoding), and once again replace the constant “class “NSASCIIStringEncodeing”” with 1.

Ric

They should if you remove the quotes and precede them by "current application’s ". It makes for longer code, but it also makes it more readable later on.

Error 1 is gone… :smiley: Error 2 is still there… :frowning: After testing with it, I can’t get anything.

I have:

on receiveaction_(sender)
		set thetweet to tweetfield's stringValue() as string
		if (thetweet's length) ≤ 140 then
			set sendpass to thepassword's stringValue()
			set senduser to theusername's stringValue()
			tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
			tell current application's class "NSMutableURLRequest" to set therequest to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
			tell current application's class "NSData" to set postdata to thetweet's dataUsingEncodeing_allowLossyConversion_(1, false)
			tell current application's class "NSString" to set postLength to (postdata's length)'s stringWithFormat_("%d")
			request's setHTTPMethod_("POST")
			request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
			request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
			request's setHTTPBody_(postdata)
		end if
	end receiveaction_

check the spelling of dataUsingEncoding_allowLossyConversion_

Thanks, but it didn’t help. I got it right this time, but it’s still a pain in my back.

Dylan,

“dataUsingEncoding_allowLossyConversion” is an instance method, not a class method, so try changing the line:

tell current application’s class “NSData” to set postdata to thetweet’s dataUsingEncoding_allowLossyConversion_(1, false) to

set postdata to thetweet’s dataUsingEncoding_allowLossyConversion_(1, false)

Ric

Tried… and nothing worked. :confused: I decided to do some research… and found out the variable “request” was not defined, either. :mad: OHZ NOZ!

I need help… I can’t understand this mumbo jumbo of errors and messes.

with

on receiveaction_(sender)
		set thetweet to tweetfield's stringValue() as string
		display dialog thetweet
		if (thetweet's length) ≤ 140 then
			set sendpass to thepassword's stringValue()
			set senduser to theusername's stringValue()
			log 1
			tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
			log 2
			tell current application's class "NSMutableURLRequest" to set therequest to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
			log 3
			set postdata to thetweet's dataUsingEncoding_allowLossyConversion_(class "NSASCIIStringEncodeing", false)
			log 4
			try
				tell current application's class "NSString" to set postLength to (postdata's length)'s stringWithFormat_("%d")
			end try
			log 5
			request's setHTTPMethod_("POST")
			log 6
			request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
			log 7
			request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
			log 8
			request's setHTTPBody_(postdata)
			log 9
		end if
	end receiveaction_

Oops. I just set the wrong variable. facepalm

OK… got this:

from

on receiveaction_(sender)
		set thetweet to tweetfield's stringValue() as string
		display dialog thetweet
		if (thetweet's length) ≤ 140 then
			set sendpass to thepassword's stringValue()
			set senduser to theusername's stringValue()
			log 1
			tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
			log 2
			tell current application's class "NSMutableURLRequest" to set request to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
			log 3
			set postdata to thetweet's dataUsingEncoding_allowLossyConversion_(1, false)
			log 4
			tell current application's class "NSString" to set postLength to (postdata's length)'s stringWithFormat_("%d")
			log 5
			request's setHTTPMethod_("POST")
			log 6
			request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
			log 7
			request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
			log 8
			request's setHTTPBody_(postdata)
			log 9
		end if
	end receiveaction_

You’ve coerced it to an AS string; you can’t then call a Cocoa method on it. Try moving the “as string” into your display dialog part.

Ah! Progress! Messed stuff around. Next, the variable postLength is not defined after “6” is logged. :confused:

on receiveaction_(sender)
		set thetweet to tweetfield's stringValue()
		display dialog thetweet as string
		if ((thetweet as string)'s length) ≤ 140 then
			set sendpass to thepassword's stringValue() as string
			set senduser to theusername's stringValue() as string
			log 1
			tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
			log 2
			tell current application's class "NSMutableURLRequest" to set request to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
			log 3
			set postdata to thetweet's dataUsingEncoding_allowLossyConversion_(1, false)
			log 4
			tell current application's class "NSString" to set postLength to (postdata's length)'s stringWithFormat_("%d") -- what does this do
			log 5
			request's setHTTPMethod_("POST")
			log 6
			request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
			log 7
			request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
			log 8
			request's setHTTPBody_(postdata)
			log 9
		end if
	end receiveaction_

Quick question: What does “NSString *postLength = [NSString stringWithFormat:@”%d", [postData length]];" do? Maybe there’s another way.

It creates a NSString object which contains the amount of bytes of the NSData object.
In AppleScript it would look like

set postLength to (length of postData) as text

Thanks~ but it can’t set the length of an object. It’s not the right data.

The AppleScript code is pseudo code. As the class «data .» is an abstract class,
it doesn’t respond to the length or count command.
You have to perform the task with ObjC methods.
I guess you have to declare postData as a NSData object with tell current application’s class “NSData”

I’ll look in the documentation.

YES! No errors, but I must be missing something because it didn’t post anything. Is my method of authorization (ex “http://dylanweber:password@twitter.com/statues/update.xml”) right?

on receiveaction_(sender)
		set thetweet to tweetfield's stringValue()
		log thetweet as string
		if ((thetweet as string)'s length) ≤ 140 then
			set sendpass to thepassword's stringValue()
			set senduser to theusername's stringValue()
			log 1
			tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
			log 2
			tell current application's class "NSMutableURLRequest" to set request to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
			log 3
			set postdata to thetweet's dataUsingEncoding_allowLossyConversion_(1, false)
			log 4
			tell current application's class "NSData" to set postLength to postdata's |length|() as string
			log 5
			request's setHTTPMethod_("POST")
			log 6
			request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
			log 7
			request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
			log 8
			request's setHTTPBody_(postdata)
			log 9
		end if
	end receiveaction_