How to read Headers Parameter on server side "RESTLET" - netsuite

[I am trying to send parameter with GET request on RESTLET Netsuite !!
but i am continuously facing issue 403 forbidden when i am trying to concatenate parameter with URL , without parameter Request is working good and getting response but i want to send parameter so now i am trying to send parameter through Header but not able to read on server side !! please help me out .how to read request header values/parameters on Netsuite RESTLET.
1-first i tried concatenate with +"&name=asd" and then Uri builder throws error 403 forbidden
2-Now i am sending parameters through request.header.add("name":"asd") working good
This is my client side request code in which i am attaching parameters in request header
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.ContentType = "application/json";
request.Method = "GET";
request.Headers.Add(header);
request.Headers.Add("email", "polutry#key-stone.co");
request.Headers.Add("password", "123-4567");
WebResponse response = request.GetResponse();///here i got 403 on concatination ////
function getCustomer(context)
{
var request =https.request.headers;///headers function not defined
// var email =request.email;
//var password = request.password;
}
Want to get header values on server side][1]
[1]: https://i.stack.imgur.com/SEB68.png

In RESTlet you don't have access to request-headers, instead you get all the arguments passed to RESTlet in scriptContext(function argument to RESTlet entry point, get in current case).
Check this out on how to use RESTlet and how you can pass arguments.
Note: Add { "Content-Type": "application/json" } header since you want to pass data to RESTlet.

Related

validateRequest method in Node.js Twilio client library not validating request while running in AWS Lambda

I am trying to validate that an http POST request to an AWS Lamdbda function URL from a Twilio HTTP Request widget inside a Twilio Studio flow truly originated from Twilio. I am using the Node.js Twilio client library, which provides the validateRequest method to accomplish what I am after. The content-type header in the Twilio Studio flows HTTP Request widget is set to application/json
The problem is that I am passing in the "x-twilio-signature" header, the url, twilio auth token, and POST params to the validateRequest method and it always returns false. Below is the code snippet used to try and accomplish this.
const authToken = process.env.twilio_auth_token
const sid = process.env.twilio_account_sid
const client = require('twilio')
exports.handler = (event) =>
{
let twilioSignature = event.headers['x-twilio-signature']
let requestBody = event.body
let requestUrl = 'https://my-function-url.io/'
let requestIsValid = client.validateRequest(authToken, twilioSignature, requestUrl, requestBody)
if(requestIsValid){
console.log('valid request')
} else {
console.log('invalid request')
}
}
Seems like someone else had a similar issue in the past. I copied parts of the answer here:
The issue here is that query string parameters are treated differently to POST body parameters when generating the signature.
Notably part 3 of the steps used to generate the request signature says:
If your request is a POST, Twilio takes all the POST fields, sorts them by alphabetically by their name, and concatenates the parameter name and value to the end of the URL (with no delimiter).

How to log the actual request header when using fetch API

Beginner question, I am using the fetch API in one of my Cloudflare Service Worker to send request to a third party; Everything works fine that I am able to send out the request and receive response; But now I have a need to log everything I sent and received, which I am not sure how exactly can I do so;
The main requirement:
Able to log the whole request header being sent
The problem is that the request header that I specified is not the full header being sent, like in the following example I only specified the content-type, but in real practice it will send with more properties added by the application itself, e.g. accept, x-forwarded-for, content-length, etc.
Request:
var toThirdPartyReqMethod = 'POST';
var toThirdPartyReqHeader = {'content-type': 'application/json'};
var toThirdPartyReqSetting = {
method: toThirdPartyReqMethod,
headers: toThirdPartyReqHeader,
body: toThirdPartyReqBodyArrayBuffer
};
var toThirdPartyUrl = "https://somewhere.com";
var fromThirdPartyResponse = await fetch(toThirdPartyUrl, toThirdPartyReqSetting)
var fromThirdPartyResponseBodyArrayBuffer = await fromThirdPartyResponse.arrayBuffer();
I am able to use fromThirdPartyResponse to digest the response; but I have no idea how to record the exact request I sent out.
I tried to do it as follows:
var oriReqHeaderContentType = fromThirdPartyResponse.request.headers.get("content-type");
But obviously it is not the right answer and returned with the following error:
TypeError: Cannot read property 'headers' of undefined
Anyone gets some idea of how to log the request being sent when using fetch API?
Sorry for asking beginner questions :(
You can access request headers like this
const contentType = req.get("Content-Type");

Netsuite-Suitelet post to restlet get parameters

I am executing a restlet by a post request in a suitelet. The restlet runs fine and I am able to pass data back from restlet--->suitelet, but I am unable to pass data from the suitelet to the restlet when I call the restlet.
suitelet
var test = 'test';
var parameters = {'custscripttest_input':test};
var apiURL='https://3864948-
sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?
script=xxx&deploy=1';
try{
var restletResponse=https.post({
url:apiURL,
headers:header,
body:postData,
params:parameters
});
restlet
var testInput =
runtime.getCurrentScript().getParameter({name:'custscripttest_input'});
No parameters show in runtime.getCurrentScript. I have created the parameter in the restlet script record.
I have also tried passing body data and retrieving in the restlet like so:
var data = context.request.parameters
I seemed to have overlooked my body:postData format. My post data body structure in my suitelet was
var postData={input:{"cust_input": test}
I retrieved data in the restlet using context.input.cust_input;
The params in an http(s) request are added to the url. They are not passed through to the script deployment parameters of the restlet.
Restlets don't give access to query string parameters when a body is included.
If you need the parameters you could format your postbody as:
body:JSON.stringify({
params:parameters,
body:postData // have postData still as an object at this point.
})

Creating Stripe Express Connect Account, Getting 'unsupported_content_type' when POST to endpoint for account ID

Im having trouble sending "POST" request to stripes token endpoint at the end of creating connect express account. Currently, my application(ios) is opening the OAuth Link (which contains a redirect uri to my server(Firebase functions)) to the express account creation web page made by stripe. Once the user completes their account set up I receive the authorization code in the backend and then use the authorization code and client_secret_key to send a POST request to "https://connect.stripe.com/oauth/token". I'm using XMLHttpRequest npm to send a POST request within firebase functions and setting my parameters like this
var params = "client_secret=" + CLIENT_SECRET + "&code=" + AUTHORIZATION_CODE + "&grant_type=authorization_code"
and my request header like this
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
Yet I get back this error after sending POST request
"error": {
"type": "invalid_request_error",
"code": "unsupported_content_type",
"message": "Invalid request: unsupported Content-Type text/plain;charset=UTF-8. If error persists and you need assistance, please contact support#stripe.com."
}
I thought I was properly setting request header content-type to "application/x-www-form-urlencoded" but still getting back error as if the content-type was "Text/plain".
Here is full backend code
exports.CreateUsersPayoutAccount = functions.https.onRequest((req, res) =>{
const userID = req.query.state
const AUTHORIZATION_CODE = req.query.code
console.log(userID)
console.log(AUTHORIZATION_CODE)
//console.log(req);
var request = new XMLHttpRequest()
request.addEventListener('load', getStripeConnectUserID)
var params = "client_secret=" + CLIENT_SECRET + "&code=" + AUTHORIZATION_CODE + "&grant_type=authorization_code"
//var params = JSON.stringify({client_secret : CLIENT_SECRET, code : AUTHORIZATION_CODE, grant_type : "authorization_code"})
console.log(params);
request.open("POST", "https://connect.stripe.com/oauth/token", true)
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
//request.setRequestHeader("Content-length", params.length)
request.send(params)
})
If anyone could point me in the right direction it would be much appreciated, I am somewhat new to writing backend HTTP calls.
In the backend cloud function, rather than attempt to send the POST request from there, write the auth code to a spot in firebase database.
Back in Xcode have a listener function that executes if there's a change in that database spot. The function should then read the change (the auth code) and simply send an easy swifty POST request with Alamofire.
I understand why you made the uri direct to your specific cloud function (since its an http trigger), but isn't the point of sending a uri so that stripe can redirect the user back to a specific app page, does the user still get directed back to the app in your case???

How to Redirect url with post data using http module in node.js

I have to redirect url i.e https://domain.com/micro with email_id=san#gmail.com as body through post/get request.
I know that We can do it by passing parameter to url with get method.but I dont want to pass in url for that I have pass as body.
this is my endpoint,
var body1 = JSON.stringify({
email_id:'san#gmail.com',
link_id:'34sd'
});
function read(request,respone){
response.statusCode = 307;
response.writeHead(307,{Location: 'https://domain.com/micro'});
response.end(body1);
}
Please can you suggest me on same .

Resources