http request - post

ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

http request - post

Post by ArielRauch »

how can I specify the header with the new http request element?

I specifically need to specify the Content-Type
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

http request - post

Post by gabrielp »

From the documentation:

The 'Content-Type' HTTP request header is automatically added to the request with the value

"application/x-www-form-urlencoded" (which means the request contains HTML form input

data).


There is an option for adding a header, but I'm not sure if Switch will be smart enough to remove the Content-Type header it already added. Maybe give this a shot anyway?

theHTTP.addHeader( "Content-Type", "application/vnd.cip4-jmf+xml" );



If that doesn't work, perhaps this before you addHeader:

theHTTP.resetHeaders();

But it looks like the resetHeaders method only works on headers added by addHeader.



If there really isn't a way to change the content type, then the HTTP class surely just got a lot less valuable and we might find ourselves stuck using cURL.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

http request - post

Post by ArielRauch »

thanks, Gabriel!



I actually was trying to use the flow element HTTP request. I assume that this is not suitable.



Where is the documentation for the HTTP class?



Thanks



Ariel
ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

http request - post

Post by ArielRauch »

ok found the updated documentation.

As far as I understand the flow element "HTTP request" does not give any option to add or change the content-type header.



Anyone?
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

http request - post

Post by gabrielp »

ArielRauch wrote: ok found the updated documentation.

As far as I understand the flow element "HTTP request" does not give any option to add or change the content-type header.



Anyone?
It appears there is an option for specifying a mime-type (which is what content-type on an HTTP request is) but I'm unclear of how it works.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

http request - post

Post by ArielRauch »

I am stuck there too:)



Dwight - please enlight(en):)
ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

http request - post

Post by ArielRauch »

ok - I think I made it work - with a script.



I added the content-type as a header and I received the response.



Haleluya - might be appropriate for those times
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

http request - post

Post by gabrielp »

ArielRauch wrote: ok - I think I made it work - with a script.



I added the content-type as a header and I received the response.
Glad it worked out and it's good to know there is a solution. I knew I would have this problem eventually once I started sending JMF with the new HTTP class.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

http request - post

Post by ArielRauch »

Below please see the very raw code of how to get response from the dfe:



function jobArrived( s : Switch, job : Job )

{

var theHTTP = new HTTP();



theHTTP.setAttachedFile( job.getPath() );

theHTTP.url = "http://172.16.200.50:8080/prodflow/jmf/dfe";

theHTTP.addHeader("Content-Type","application/vnd.cip4-jmf+xml");

theHTTP.post();



job.log( 3, "Upload started", 100);

while( !theHTTP.waitForFinished( 3 ) ) {

job.log( 3, "Uploading...", theHTTP.progress() );

}

job.log( 3, "Upload finished" );

job.log( 3, "Server response: %1", theHTTP.getServerResponse().toString( "UTF-8" ) );

if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 ) {

job.log( 3, "Upload completed successfully" );

}

else {

job.fail( "Upload failed with the status code %1", theHTTP.statusCode );

return;

}

job.sendToSingle( job.getPath() );

}







The asynchronous part which is solved with the while loop is not very elegant.

I think we need to think about it a bit
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

http request - post

Post by gabrielp »

ArielRauch wrote: Below please see the very raw code of how to get response from the dfe:
Very cool. It's nice to see it in action. Thanks for sharing!
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

http request - post

Post by freddyp »

ArielRauch wrote: The asynchronous part which is solved with the while loop is not very elegant.

I think we need to think about it a bit


Do not think about it: the post, put, get and del functions of an HTTP object are executed asynchronously. That is how it is.



I am glad to see that the new HTTP class is quickly finding some useful applications in the Switch community :)



Freddy
actionHero
Member
Posts: 37
Joined: Thu May 18, 2017 12:06 am

Re: http request - post

Post by actionHero »

Hello,
I'm trying to PUT to my AWS API Gateway. I have a Lambda Function that doesn't require the AWS Keys to perform the PUT. This works in Postman perfectly. In Switch I get "HTTP PUT request error: error during handshake[2]: 0x80090326" which is an error in authentication.

Any Ideas?

function jobArrived( s : Switch, job : Job )
{
var aws = new HTTP( HTTP.SSL );
aws.url = "https://My_AWS_API";
aws.authScheme = HTTP.NoneAuth;
aws.setAttachedFile( job.getPath() );
var theResponseFilePath = job.createPathWithName( job.getNameProper(), false );
aws.localFilePath = theResponseFilePath;
aws.addHeader( "Content-Type", "application/json" );
aws.addHeader( "Authorization", "allow" );
aws.put();
while( !aws.waitForFinished( 3 ) ) { }

job.log( 1, "Request finish status: %1", aws.finishedStatus );

job.sendToSingle( theResponseFilePath );
}
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: http request - post

Post by gabrielp »

It doesn't give you a response code or body from the server? You don't need to provide any authentication for your API Gateway?
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
actionHero
Member
Posts: 37
Joined: Thu May 18, 2017 12:06 am

Re: http request - post

Post by actionHero »

The error I am getting is "HTTP GET request error: error during handshake[2]: 0x80090326" It's returning HTTP error code 0, which is not a valid error. Which leads me to believe that it's not authenticating.

Lambda is authenticating based on the header "Authorization:allow".

Postman code that works:
PUT /test/CustomerID HTTP/1.1
Host: blahblah.execute-api.us-east-1.amazonaws.com
Content-Type: application/json
Authorization: allow
Cache-Control: no-cache
Postman-Token: e861a868-05aa-fb76-618a-b81a7226ce01

{
"CustomerID": "000100",
"CustomerNm": "AWS is NOT Cool",
"Date": "01/12/2017",
"Shipping": "USPS",
"commentId": "Hello World"
}
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: http request - post

Post by gabrielp »

An HTTP request should have an HTTP status code and message on response.

Try and print it out to see what it is:

Code: Select all

 if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200
          && File.exists( theHTTP.localFilePath ) )
  {
      job.log( 1, "Request completed successfully" );
  }
  else
  {
      job.fail( "Request failed with the status code %1", theHTTP.statusCode );
      return;
   }
}
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
Post Reply