I have to send some data to API but autorization to this api is thru cookie. So for sending data I have to send my username and password and in response I get cookie and in response body I get element "sessionID" which containts string value for that cookie. In every next API request I need to use that cookie (or create it with stored string value from first API request). How to make/integrate cookie functionality inside my existing procedures for API requests? If I do requests using Postman application, then it automaticly saves cookie from first request and use it on every next request.
So is solution for my problem :
to save a cookie and use it for every next request?
to save string value and create cookie for every next request?
to store cookie into session and use cookie from session?
something else I didn't thought of?
My procedure for sending GET request is below.
USING System.Xml.* FROM ASSEMBLY.
USING System.Net.* FROM ASSEMBLY.
USING System.Collections.Specialized.* FROM ASSEMBLY.
ROUTINE-LEVEL ON ERROR UNDO, THROW.
/* *************************** Definitions ************************** */
DEFINE INPUT PARAMETER cLink AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER cUser AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER cPass AS CHARACTER NO-UNDO.
DEFINE OUTPUT PARAMETER lcResponse AS LONGCHAR NO-UNDO.
DEFINE VARIABLE wcClient AS System.Net.WebClient NO-UNDO.
DEFINE VARIABLE nvValues AS System.Collections.Specialized.NameValueCollection NO-UNDO.
DEFINE VARIABLE whcResponse AS System.Net.WebHeaderCollection NO-UNDO.
DEFINE VARIABLE rsResponse AS "System.Byte[]" NO-UNDO.
DEFINE VARIABLE cRespHeader AS CHARACTER NO-UNDO.
DEFINE VARIABLE wpProxy AS System.Net.WebProxy NO-UNDO.
/* *************************** Main Block *************************** */
wcClient = NEW System.Net.WebClient().
nvValues = NEW System.Collections.Specialized.NameValueCollection().
ASSIGN
nvValues["username"] = cUser
nvValues["password"] = cPass
.
rsResponse = wcClient:UploadValues(cLink, nvValues).
lcResponse = System.Text.Encoding:Default:GetString(rsResponse).
cRespHeader = System.Text.Encoding:Default:GetString(rsResponse).
DELETE OBJECT nvValues.
DELETE OBJECT rsResponse.
DELETE OBJECT wcClient.
Found some C# code for cookies but don't know how to integrate that into my procedure.
CookieContainer cookieContainer = new CookieContainer();
System.Net.Cookie userNameCookie = new System.Net.Cookie("user", "username");
System.Net.Cookie passwordCookie = new System.Net.Cookie("password", "9848jf7s7ejhd");
cookieContainer.Add(userNameCookie);
cookieContainer.Add(passwordCookie);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(produkt);
request.CookieContainer = cookieContainer;
Thanks in advance
Solution for this problem is next
wcClient:Headers:ADD("Cookie","value_of_cookie_including_name").
Spent lots of hours testing and finally it worked. Hope that this answer someone saves a lot of time.
Related
I have a little problem with my outlook interface.
I'm using <outlook\msoutl.tlh> to create and fill an e-mail item automatically.
But now I want to display my signature too.
I noticed that if I don't set the mail body, the signature is added automatically. But as soon as I want to set the mail body, the signature is not shown. It seems that it will be overwritten. So I tried the following :
string standardText = "Hello Outlook"
string signature = IMailItemPtr->Body;
IMailItemPtr->Body = standardText + signature
So first the signature has to be cached and then appended to the standard text for the body.
But here I noticed that the body is empty until set (which is logical).
But now I wonder where my signature is stored and how I can get it?
Here is the important part of my code:
string standardText = "Hello Outlook"
Outlook::_ApplicationPtr spOutlook(__uuidof(Outlook::Application));
// Get the MAPI namespace
Outlook::_NameSpacePtr pMAPI = spOutlook->GetNamespace("MAPI");
// Initiate a new Outlook-session
pMAPI->Logon("", "", false, true); // Log on by using the default profile or existing session (no dialog box).
// Query the MailItem interface
Outlook::_MailItemPtr IMailItemPtr = spOutlook->CreateItem(Outlook::olMailItem);
IMailItemPtr->Subject = mailSubject;
IMailItemPtr->Body = standardText.c_str();
I get an error when trying to extract a value from a JSON response body in Postman.
ReferenceError: teste is not defined
This is what I have tried:
var jsonData = JSON.parse(responseBody);
pm.globals.set("access_token",jsonData.access_token)
** pm.globals.set("x-teste-msg-sign",jsonData.x-teste-msg-sign)
It's more than likely to be this, judging by the way you're extracting the access_token
pm.globals.set("x-teste-msg-sign", jsonData["x-teste-msg-sign"])
As the key contains the - character, you would need to use bracket notion rather than dot notion to access the value.
Here's an example:
let jsonData = {
"x-teste-msg-sign": 12345
}
console.log(jsonData.x-teste-msg-sign) // This would cause a script error
console.log(jsonData["x-teste-msg-sign"]) // This would set the value to the variable
Epicor ERP 10.2.500 has been recently released with the addition of Epicor Functions. They can be called from within Method and Data Directives.
Do anybody has been able to do so with a Form Customization within Epicor?
This is possible via a REST call to your function API. In this case, I had a function that sent an email from some inputs.
private void epiButtonC1_Click(object sender, System.EventArgs args)
{
//API Key is included in the query param in this example.
var request = (HttpWebRequest)WebRequest.Create("https://{appserver}/{EpicorInstance}/api/v2/efx/{CompanyID}/{LibraryID}/{functionName}/?api-key={yourAPIKey}");
request.Method = "POST";
//All REST v2 requests also sent with authentication method (Token, Basic)
//This should be Base64 encoded
string username = "userName";
string password = "passWord";
string encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
//Add body to correspond to request signature
request.ContentType = "application/json";
using(var writer = new StreamWriter(request.GetRequestStream()))
{
var values = new Dictionary<string, string>;
{
{"toEmailAddress", "someEmail#email.com"},
{"fromEmailAddress","someOtherEmail#email.com"},
{"body","This is the body"},
{"subject","Hello from Client Code!"}
};
string json = JsonConvert.SerializeObject(values);
writer.Write(json);
}
using (var response = request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
var result = reader.ReadToEnd();
epiTextBoxC1.Text = result.ToString();
}
}
Haven't done it myself personally, but looking into the first post release notes about it here leads me to believe there is no out of the box solution, yet in this version/initial release.
However I'm sure you could do a HTTP request from within the Epicor customization if you have the REST API enabled in your environment.
If you create your own dll that calls the EpicorFunction you can use it within the customization. Still looking for a way to call them directly.
REST endpoint is the recommended way to perform the function call as pointed out by a-moreng.
If for some reason you cannot use this, you can use a passthrough method to any server-side BO via a customization Adapter. For instance, create an updatable BAQ which you can call from a customization using the DynamicQueryAdapter.
Pick an arbitrary table and field to save the BAQ.
Create three string parameters to store the Function library name, the function name, and a delimited list of parameters.
On the GetList method, create a Base Processing Directive.
Split your delimited parameter list and convert them to the appropriate datatypes.
Use the resulting variables to call your function.
If desired, you can pass return variables into the ttResults of the BAQ
Using a GET in postman with the URL posted below, I am able to store the entire response header in question with all of its data in a var, the issue for me is how do I verify the pieces of data inside that var
here is my URL
http://localhost/v1/accounts?pageNumber=1&pageSize=2
[
using postman I am able to get the above in a var
var XPaginationData = postman.getResponseHeader(pm.globals.get("PaginationHeader"));
pm.globals.set("XPaginationData", XPaginationData);
is there a way to get the individual values inside the response header X-Pagination stored in a different var to assert later
using this in postman
pm.globals.set("XPaginationData", JSON.stringify(pm.response.headers));
console.log(JSON.parse(pm.globals.get('XPaginationData')));
console.log(JSON.parse(pm.globals.get('XPaginationData'))[4].value);
I get
how would i go about getting "TotalCount" for example
BIG EDIT:
thanks to a coworker, the solution is this
//Filtering Response Headers to get PaginationHeader
var filteredHeaders = pm.response.headers.all()
.filter(headerObj => {
return headerObj.key == pm.globals.get("PaginationHeader");
});
// JSON parse the string of the requested response header
// from var filteredHeaders
var paginationObj = filteredHeaders[0].value;
paginationObj = JSON.parse(paginationObj);
//Stores global variable for nextpageURL
var nextPageURL = paginationObj.NextPageLink;
postman.setGlobalVariable("nextPageURL", nextPageURL);
You could use JSON.stringfy() when saving the environment variable and then use JSON.parse() to access the different properties or property that you need.
If you set a global variable for the response headers like this:
pm.globals.set('PaginationHeader', JSON.stringify(pm.response.headers))
Then you can get any of the data from the variable like this:
console.log(JSON.parse(pm.globals.get('PaginationHeader'))[1].value)
The image shows how this works in Postman. The ordering of the headers returned in the console is inconsistent so you will need to find the correct one to extract data from the X-Pagination header
Looks like an issue with Postman itself.
The only solution that worked for me was to stringify & parse the JSON again, like this:
var response = JSON.parse(JSON.stringify(res))
After doing this, the headers and all other properties are accessible as expected.
My cookie will not set the full string "A&B". When I request it, I only get
"A". Do I need to wrap it with something so it reads the "&", as I wonder if this character is breaking it.
controller:
Response.Cookies["Order"]["PrincId"] = model.Princ_ID.ToString();
view:
var session_princid = Request.Cookies["Order"]["PrincId"];
#session_princid