Sunday, September 15, 2019

#1 2019-04-02 11:24:26 am

jcwelch
Member
Registered: 2009-09-11
Posts: 57

NSURL oddity

So I'm looking to move an app away from using do shell script for curl operations.

I'm hitting something that seems odd, wherein when I create a URL with NSURL's URLWithString, what I'm getting back is the result of the URL on the website, not a URL object?

This isn't bad in and of itself for GET operations, but if it's a URL for a post, where there's nothing returned when "running' the URL, then I get missing value.

I think I'm missing something?

Offline

 

#2 2019-04-03 02:18:30 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 97

Re: NSURL oddity

You probably want to be using NSURLSession's or NSURLConnection's methods and related classes.

Offline

 

#3 2019-04-03 04:57:24 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5881

Re: NSURL oddity

Of the two, NSURLConnection is deprecated, but I suspect it will be supported for a while. And it's easier to use from AppleScript, partly because it has a synchronous option. Simpler stuff can be done like this:

Applescript:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theNSURL to current application's |NSURL|'s URLWithString:theString
set theNSURLRequest to current application's NSURLRequest's alloc()'s initWithURL:theNSURL cachePolicy:(current application's NSURLRequestReloadIgnoringCacheData) timeoutInterval:5.0
set {theNSURLConnection, theResponse, theError} to current application's NSURLConnection's sendSynchronousRequest:theNSURLRequest returningResponse:(reference) |error|:(reference)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2019-04-04 09:11:52 am

jcwelch
Member
Registered: 2009-09-11
Posts: 57

Re: NSURL oddity

Okay, so next question, how do you set the HTTP Method (get/post) etc. I've tried a few options, none of them seem to be working. (lots of unrecogonized selectors)

Offline

 

#5 2019-04-04 05:23:52 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5881

Re: NSURL oddity

You'll find quite a few examples here:

http://piyocast.com/as/

The site is in Japanese, but obviously the code isn't, and the site is searchable.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#6 2019-04-11 12:42:19 am

maro
Member
From:: Nerima, Tokyo, Japan
Registered: 2004-05-30
Posts: 21
Website

Re: NSURL oddity

I've wrote various REST API calling code. NSURLSession is not faster than NSURLConnection.
NSURLSession sample is here.

--Call Postmark’s spam detection API
http://piyocast.com/as/archives/3600

And other samples are ...
http://piyocast.com/as/?s=NSURLSession


I wrote thousands of AppleScript to realize my idea. Natural language interface, voice recognition commander and so on. Though my mother toungue is strange language, Japanese, my most frequently write language is AppleScript. I believe it is for making things easy and powerful.

Offline

 

#7 2019-05-19 04:53:03 am

technomorph
Member
Registered: 2017-12-14
Posts: 118

Re: NSURL oddity

Shane Stanley wrote:



Applescript:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theNSURL to current application's |NSURL|'s URLWithString:theString
set theNSURLRequest to current application's NSURLRequest's alloc()'s initWithURL:theNSURL cachePolicy:(current application's NSURLRequestReloadIgnoringCacheData) timeoutInterval:5.0
set {theNSURLConnection, theResponse, theError} to current application's NSURLConnection's sendSynchronousRequest:theNSURLRequest returningResponse:(reference) |error|:(reference)




Shane could you explain the cache policy option you choose?

Thanks

Offline

 

#8 2019-05-19 04:57:28 am

technomorph
Member
Registered: 2017-12-14
Posts: 118

Re: NSURL oddity

jcwelch wrote:

Okay, so next question, how do you set the HTTP Method (get/post) etc. I've tried a few options, none of them seem to be working. (lots of unrecogonized selectors)



Here's some
Examples pasted from Piyumo site

Applescript:


aRequest’s setHTTPMethod:"POST"
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
aRequest’s setHTTPShouldHandleCookies:false
aRequest’s setTimeoutInterval:timeoutSec
–aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding"–Does not work with this API & Method
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
aRequest’s setValue:"application/json; charset=UTF-8" forHTTPHeaderField:"Content-Type"

Offline

 

#9 2019-05-19 05:41:35 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5881

Re: NSURL oddity

technomorph wrote:

Shane could you explain the cache policy option you choose?



It's pretty much what its name says: force reload ignoring anything cached.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)