Hi,
We have been given access to a web service that allows us to create jobs in SAP. It requires that we POST JSON to a given URL and the theory is that the result of the query is the Job Number of the newly created job.
I am new to ApplescriptObjC, but it seems this is achievable in Obj-C as I have been given a link to get me started:
http://stackoverflow.com/questions/7673127/how-to-send-post-and-get-request
I have been following a similar post at http://macscripter.net/viewtopic.php?id=33338, that appears to use the same terminology. I have butchered together something that does not error, but I don’t know if it worked either. It seems the critical bit is making the POST query and reading the result
property NSString : current application's class "NSString"
property NSData : current application's class "NSData"
property senduser : "test"
property sendpass : "test"
on createJSON_(sender)
--NSString *post = [NSString stringWithFormat:@"test=Message&this=isNotReal"]
log 1
-- choose the JSON file
set chosenFile to posix path of (choose file)
set aURL to current application's |NSURL|'s fileURLWithPath:chosenFile
set theJSONDoc to current application's NSXMLDocument's alloc()'s initWithContentsOfURL:aURL options:0 |error|:(missing value)
log 2
-- expects it in text format
set theJSONDoc to convertAnyToText(theJSONDoc)
set post to NSString's stringWithFormat_(theJSONDoc)
--NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
log 3
set postData to post's dataUsingEncoding_allowLossyConversion_(1, true)
--NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]
--set postLength to NSString's (postData's length)'s stringWithFormat_("%d")
log 4
tell current application's class "NSString" to set postLength to NSString's stringWithFormat_("%d", postdata's length)
log 5
--NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
--[request setURL:[NSURL URLWithString:@"http://YourURL.com/FakeURL"]];
--[request setHTTPMethod:@"POST"];
--[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
--[request setHTTPBody];
tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@my.url.com")
log 6
tell current application's class "NSMutableURLRequest" to set request to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
request's setHTTPMethod_("POST")
request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
request's setHTTPBody_(postData)
log 7
--NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
--[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
--NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
--NSLog(@"requestReply: %@", requestReply);
--}] resume]
set session to current application's NSURLSession's sessionWithConfiguration:(missing value)
session's setDelegate:me -- so script gets told when it's done
set theTask to session's dataTaskWithRequest:request
theTask's resume() -- start the task
end
I can’t say I am proud of it, but it gets through from start to finish. The commented out bits are the original code in Obj-C
I think the trick is to figure out the response but that last NSURLSession bit is so nested and unfathomable to me I have no idea how to write it
If you can give any tips how to tidy up the above code, point out if I am going wrong somewhere, or unravel the NSURLSession command, I would be most grateful
Ian