Monday, April 22, 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: 55

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: 5626

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: 5626

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: 3
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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)