How to get token value of string type from rest response - rest

Delphi 10.4.2
REST server generates new tokens every 2-3 hours and client must login in order to get new token. In Swagger and Postman server returns HTTP 200:
Response of server is uuid string like "d14e02b7-ae5e-11ed-8105-005056b4f1d1"
I create new query parameters on page "Parameters" of Rest Debugger. Server returns HTTP 200 but in the tab "Tabular data" if I click on the value caption to select root element and then press "Apply" button get error:
Response RootElement, "value", is not a valid path for the response JSON.
If I place rest components(RecsClient, RestRequest, RestResponse, RestAdapter) on the form and RootElement property of RestResponse is set to 'value' or 'resultData' I get the same error.
But if I empty RootElement property of RestResponse component and execute RestRequest component I get HTTP 200 and token value appears in FDMem component but how then from RestResponse extract uuid single string value?
SOLUTION:
this code works:
var token:String;
token:=RestResponse1.JSONValue.Value;
or this:
var
JV: TJSONValue;
str1,str2: string;
begin
str1:= fRefGoods.rrespLogin.Content;
JV:=TJSONObject.ParseJSONValue(str1);
str2:=JV.Value;
the confusion was caused by not knowing that "d14e02b7-ae5e-11ed-8105-005056b4f1d1" is a valid JSON-string. I thought it was necessary JSON-string be enclosed in {} curly brackets and data presented as JSON-Pairs separated by commas. As it turned out I was wrong.
JSONLint also identifies it as valid JSON-string.

Related

How to access etag header from response in Cosmos DB post-trigger

I am writing a Cosmos DB post-trigger (NOT the Azure Function Trigger kind but the in-server JS one). In this trigger, I need to access the etag header generated in the response. The trigger works fine except when I add the following code :
// Retrieve item's new etag from the response
var newETag = __.response.getValue("etag");
This code causes the request to fail with the following exception : "Unable to get property 'value' of undefined or null reference at getValueInternal". I checked that the response is not null and that the getValue function exists and in fact the exception is thrown from within the getValue function because the property for etag does not exist.
The doc (found here) however states that:
getValue(key) → {string} Gets a specified response header value.
And other doc (found here) also states that:
The following response headers are common to all responses from the SQL API: ... etag (The etag header [...] has the same value as the _etag property in the response body.)...
Now I also DID confirm that I could indeed access the etag from the __.response.getBody() but I cannot rely on that option because my requests are made with the EnableContentResponseOnWrite = false option in which case the response body is null.
Can someone help me figure out what I am missing or doing wrong here ?

Shopee OpenAPI 1.0 Authorization and Authentication always return “No partner_id”

I am following the OpenAPI 1.0 authorization guide here (https://open.shopee.com/documents?module=63&type=2&id=53). I did the shop authorization and it successfully returning the code and shop_ID in the redirect URL.
Next I was trying the API request authentication to get shop info. The signature base string I created was this:
https://partner.uat.shopeemobile.com/api/v1/shop/get|{"partner_id": XXXXXX, "shopid": XXXXXXXXX, "timestamp": XXXXXXXXXX}
I combined this signature base string with the test key that I got from creating an app to produce the hex output. This output is placed on the Authorization header in my POST request along with these parameters in the body: partner_id, shopid, and timestamp. The result I got was always this:
{"request_id":"595b9b4bdb0b056392f8a44834dd06e7","msg":"partner_id is invalid, should be an integer between 0 and 4294967295","error":"error_param"}
I also tried this POST request without any header, but the result is always the same. Where is the mistake?
In this error you should be use partner_id of integer, not string.

How to use Property Transfer to pass refresh_token from Response to request with concatenation : TestSuite soapui

When i use jsonPath to get value of refresh token + concatenation with grant_type=refresh_token, the result was only the value of token
My source is a POST request. This POST request returns me a JSON. I need to pass the value of refresh_token with grant-type=refresh_token to the next POST request.
The expression in your Target box needs to be an XPath or JsonPath expression of where you'd like the property to end up in your next request. I don't know the shape of your messages, so one possible solution is to create, say, a Test Case property called refresh_token and transfer the value of your token there:
Then, in your next request, you could use this property with your concatenation string like this:
${= 'grant_type=refresh_token&refresh_token=' + context.expand( '${#TestCase#refresh_token}'}

How do I add a field for a header for an authentication token for Swagger UI?

My team has just started creating RESTful services for data that has previously been handled by a large monolithic legacy application. We want to document the api with Swagger UI and I have set up with one problem.
I need to pass a SAML token as a header parameter, otherwise when we try to click on the "Try it out!" button I get a 401 Authentication error. How do I add a field to the Swagger UI so that someone can put a String for a SAML token to be sent in the request?
This is actually really easy. I saw references to the answer in the documentation but I didn't really understand what it was saying. There is a field at the top next to where your service URL goes and you can use that field to input a string to pass as a header value. That input field has an id of #input_apiKey.
Then in the index.html file you just add a line to the addApiKeyAuthorization() javascript function telling it to take the value of that field and pass it as whatever value you need.
Example:
function addApiKeyAuthorization(){
var key = $('#input_apiKey')[0].value;
if(key && key.trim() != "") {
swaggerUi.api.clientAuthorizations.add("samlToken", new SwaggerClient.ApiKeyAuthorization("samlToken", key, "header"));
swaggerUi.api.clientAuthorizations.add("Content-Type", new SwaggerClient.ApiKeyAuthorization("Content-Type", "application/json", "header"));
swaggerUi.api.clientAuthorizations.add("Accept", new SwaggerClient.ApiKeyAuthorization("Accept", "application/json", "header"));
}
}
$('#input_apiKey').change(addApiKeyAuthorization);
This sets the Content-Type and Accept headers to the same values for every request, and takes the value in that input field at the top of the page in the green header and sets it as my SAML token. So now if I paste in a valid SAML string my request works and I get data back!

How to get the location header in Google Apps Script

I was experimenting with building a Google Spreadsheet where:
Column A is a list of URLs that should be 301 redirected
Column B was our intended URL to be redirect to
Column C is the server response code when hitting Column A
Column D is the URL that was ultimately served up when hitting Column A
Column E is a Boolean for B and D matching
I'm having problems with populating column D. Here is what I tried:
function getHeaders(tURL) {
var response = UrlFetchApp.fetch(tURL);
var tHeaders = response.getAllHeaders();
}
I was expecting that "location" would be in the response header. But it seems this method only returns a handful of server response headers. I get: X-Frame-Options, Date, P3P, Content-Length, Expires, X-XSS-Protection, Content-Encoding, Alternate-Protocol, Set-Cookie (array), Content-Type, Server, Cache-Control.
Question
Any suggestions on another way to get to location? Or an alternate idea on how to determine where the url was redirected to?
You need to use fetch(url, params) method of UrlFetchApp class and supply followRedirects parameter set to false:
var response = UrlFetchApp.fetch(tURL, {'followRedirects':false});
followRedirects defaults to true, and thus your request returns the headers and content of the page it was redirected to, not the requested page. That is why you do not see the Location header there.