Page 1 of 1

HTTP Request - getServerResponse

Posted: Wed Mar 30, 2016 6:24 pm
by jan_suhr
I'm working on a file upload script were you login and get back a Unique Session ID that is used for the file upload, the URL should be http://path.to.server/upload/unique-id/filename

Code: Select all

		theHTTP.url = "http://path.to.server/initialize";
		theHTTP.authScheme = (HTTP.BasicAuth);
		theHTTP.authorization = ("authorization string");
		theHTTP.user = userName;
		theHTTP.password = password;
				
		theHTTP.post(); 
The above section doesn't give any errors from the server.

I've tried the waitForFinished with the getServerResponse after that to pickup the UniqueID. The script passes the While part.
The response will come back as JSON, from the server.

Code: Select all

while( !theHTTP.waitForFinished( 3 ) )  {
			var uuid = theHTTP.getServerResponse().toString( "UTF-8" );
			theHTTP.setAttachedFile(jobFile);
			job.log( 1, "UUID = " + uuid);
			}
Any advice?

thanks

Jan

Re: HTTP Request - getServerResponse

Posted: Wed Mar 30, 2016 7:33 pm
by gabrielp
Could you better describe the problem? Are you not getting a unique ID from the first request or are you having trouble with the second request?

Do you really POST to the server to get a session ID? I would think it would be GET. What does the job.log line log out? Nothing? Undefined?

Have you heard of Postman? It's very handy for testing HTTP requests.

Re: HTTP Request - getServerResponse

Posted: Wed Mar 30, 2016 9:25 pm
by jan_suhr
It is supposed to first do an initialize in the first POST, then it will send back the uuid that has to be used in the upload URL.

The logline shows nothing so the while loop doesn't run.

I'll check the Postman. I'm new to this kind of stuff and need all helpful tools that are available.

Thanks

Jan

Re: HTTP Request - getServerResponse

Posted: Wed Mar 30, 2016 9:46 pm
by jan_suhr
I got a step further, the manual they have sent me for the API is not correct, in a email they have a completely different URL-string to use and then it works in both Switch and Postman. It was with this string a GET that would be used. Apparently the RTFM isn't always true :cry:

It now give me a JSON-string as expected.

Thanks for the tip on Postman, really useful.


Jan

Re: HTTP Request - getServerResponse

Posted: Thu Mar 31, 2016 12:36 am
by gabrielp
Glad you got it worked out. Postman is awesome.

Re: HTTP Request - getServerResponse

Posted: Sat Apr 02, 2016 11:08 pm
by jan_suhr
OK, I got the correct documentation for this project.

They sent me a PHP-file that's doing the work running from a webpage. I want to create the same thing from within Switch.

What I'm now struggling with is a function that send info and will get a UUID back that is then used for uploading files.

They use in the PHP script a cURL command to create the parameters for the request for the UUID.

Code: Select all

	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
	
	$body = curl_exec($ch);
Is there a way to do the same with JavaScript? What I've understand is that the HTTP Request element in Switch do this? But for the URL to send to get the UUID they need the $body variable in the GET URL command. Is there a way to get the $body equivalent for me to use?


Thanks

Jan

Re: HTTP Request - getServerResponse

Posted: Mon Apr 04, 2016 4:05 am
by gabrielp
I can help describe what some of this is doing, but it's been a while since I've used PHP's cURL classes. I'm pretty sure cURL uses GET by default, so this is a GET request to $url with "basic" HTTP authentication. Basic HTTP authentication is usually an HTTP header option called "Authorization : Basic " with the username and password base64 encoded with the following format: "[username]:[password]".

So... fire up Postman and try a GET request to that URL. Use the "Authorization" tab in Postman to set your username and password. Once you get that working, you can see what the response body looks like.

When you have a good understanding of the call, then you can use Switch's HTTP class (if you're using Scripter) to write a script to pull a session down. You'd have to use Switch's Scripter documentation to see how to set headers or use basic authentication for a request. Alternatively, you can use Scripter to execute a cURL script directly or even fire your PHP script, if a PHP runtime is installed on the Switch server. You may have luck getting this to work with Switch's HTTP configurator, but I haven't used it much.

Re: HTTP Request - getServerResponse

Posted: Tue Apr 05, 2016 9:29 am
by jan_suhr
Thanks for the reply.

I got that part to work with the login and get the proper response from the server, but in the next step there is an initialize step that requires the Basic sting as a part of the URL.

As I have understand it this is handled in the background by PostMan and Switch.

In the documentation it says that:
The authScheme property defines the authentication scheme used. In the case of HTTP Basic Authentication (default), the user and password are automatically Base64 encoded by Switch, and the result in the form "Basic [encoded-user-password]" is used for authorization.
And it handled by these lines of codes:

Code: Select all

		theHTTP.authScheme = (HTTP.BasicAuth);
		theHTTP.authorization = ("authorization string");
		theHTTP.user = userName;
		theHTTP.password = password;
I will need the "authorization string" as a variable after it has been encoded by Switch, is that possible?


Jan

Re: HTTP Request - getServerResponse

Posted: Tue Apr 05, 2016 4:02 pm
by gabrielp
So what was returned from the first request? A session ID or something? Did you save that value to a variable?

Does the second request need that session ID (which was returned from the first request) in the "Authorization" header?

Re: HTTP Request - getServerResponse

Posted: Thu Apr 07, 2016 8:38 am
by jan_suhr
No, the first request is a login. Then the second request is asking for a unique ID an din the second request you send the Basic Auth string from (username:password). So you create the login a second time and you need the encoded string to send in the url-request. If that is successful you get a unique ID back as JSON (UUID).

You then need this UUID in the path when you upload files.


Jan

Re: HTTP Request - getServerResponse

Posted: Thu Apr 07, 2016 3:15 pm
by gabrielp
I still don't understand the purpose of the first request (login) or what it returns.

You'll want to walk through the behavior in Postman. Once you understand how it works and you can recreate it there, it should be easy to re-create using Switch's HTTP class in a script.

Re: HTTP Request - getServerResponse

Posted: Fri Apr 08, 2016 1:21 pm
by jan_suhr
Well I got part 2 working, the URL I had wasn't the correct one :cry:

But I can't replicate it in Postman but I'm probably doing something wrong there.


Jan