When i pass the body in the GET request to the below api its throwing the 400 bad request.if i pass through the Feign client its throwing method not found it is internally converting GET reuest to POST request, Can u please suggest what are the possible ways i can do? except from changing the request to POST
Api
#GetMapping("/users")
#ApiOperation(value = "Retrieve users by role names list", notes = "Retrieves users by role name or role names passed in the list")
#ApiResponses(value = { #ApiResponse(code = 404, message = "ROLE_NOT_FOUND") })
public PagedResources<Resource<UserResponse>> retrieveUsersByRoleNames(#RequestBody RoleNameRequest roleNameRequest,
#RequestParam(name = "includeLocked", required = false) Boolean locked,
#RequestParam(name = "excludeUserId", required = false) String userId,
#RequestParam(name = "includeDeleted", required = false) Boolean isDeleted,
Pageable page,
PagedResourcesAssembler<UserResponse> pagedAssembler) {
Subscription subscription = serviceUtility.fetchSubscription();
Page<UserResponse> pageUserResponse = roleService.findAllByRoleNames(roleNameRequest, locked, isDeleted, userId, subscription,page)
.map(userAttribute -> ModelConverter.modelResponse(userAttribute, true));
log.info("pageUserResponse : " + pageUserResponse);
log.info("pageUserResponse.getTotalElements() : " + pageUserResponse.getTotalElements());
log.info("page details size for roleNames list: " + pageUserResponse.getContent().size());
log.info("page details values for roleNames list: " + pageUserResponse.getContent().toString());
return pagedAssembler.toResource(pageUserResponse);
}
Request
RoleNameRequest roleNameRequest = new RoleNameRequest();
roleNameRequest.setRoleNames(identity.getPrimaryRoles());
List<UserResponseWithWorkload> usersWithWorkload = null;
try {
log.info("Pager Details : " + pageable.toString());
log.info("subscriptionId : " + subscriptionId);
causing exception -> resources = securityServiceFeignClient.retrieveUsersByMultipleRoles(roleNameRequest,
subscriptionId, pageable.getPageNumber(), pageable.getPageSize(),userId);
usersWithWorkload = getUsersWithWorkload(resources.getContent().toArray(),
workload, identity.getActivityId(), identity.getAssignmentMode());
log.info(" Resource from security service : " + resources.getContent().toString());
log.info(" Resource size from security service : " + resources.getContent().size());
log.info(" Resource array from security service : " + resources.getContent().toArray());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
Try using #ModelAttribute annotation
#ModelAttribute("myObject") MyObject myObject
Related
I am currently new to Jmeter, and trying to create a Jmeter script to test how long a request takes to process and complete.
a) Authenticate using Token - Complete
b) Post Request - Complete - Returns 200
c) Get Request - Partially Completed
C: I am Trying to get be able to monitor this request to find out when its either completed failed etc.
I have created the Http Request Sample with a Get Request
I am able to get the Request 200 but it doesn't wait for completion
So running this in a console app, it waits for a certain time checking for status....
Is there a way to possibly write a code similar to the C# code in bean shell or groovy to wait. I was reading about while controller as well...
var result = WaitForBuildToComplete(dest, requestData, token, timeout);
static string GetStatus(string path, Token token)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(path);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
AddToken(token, httpWebRequest);
WebResponse response = httpWebRequest.GetResponse();
string responseFromServer = "";
using (Stream dataStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
}
// Close the response.
response.Close();
return responseFromServer;
}
static int WaitForBuildToComplete(string dest, RequestData requestData, Token token, int
timeout)
{
if (timeout <= 0) return 0;
var path = $"{ConfigurationManager.AppSettings[dest]}/policy?id={requestData.id}";
var startTime = DateTime.Now;
do
{
var status = GetStatus(path, token);
var msg = JsonConvert.DeserializeObject<string>(status);
var requestStatus = JsonConvert.DeserializeObject<RequestStatus>(msg);
if (!string.IsNullOrEmpty(requestStatus.DllUrl))
{
Console.WriteLine($"\nResult dll at: {requestStatus.DllUrl}");
return 0;
}
if (requestStatus.Status.ToUpper() == "FAILED")
{
Console.WriteLine($"\nFAILED");
Console.WriteLine(requestStatus.Message);
return -1;
}
if (requestStatus.Status.ToUpper() == "FAILED_DATA_ERROR")
{
Console.WriteLine($"\nFAILED_DATA_ERROR");
Console.WriteLine(requestStatus.Message);
return -1;
}
if (requestStatus.Status.ToUpper() == "NOT_NEEDED")
{
Console.WriteLine($"\nNOT_NEEDED");
Console.WriteLine(requestStatus.Message);
return -1;
}
Console.Write(".");
System.Threading.Thread.Sleep(1000);
} while ((DateTime.Now - startTime).TotalSeconds < timeout);
Console.WriteLine("Time out waiting for dll.");
return -1;
}
I started by looking at JSR223 Sampler but wanted to see if there is a better and easier way to accomplish this.
List<String> sendRequest(String url, String method, Map<String,Object> body) {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(2000)
.setSocketTimeout(3000)
.build();
StringEntity entity = new StringEntity(new Gson().toJson(body), "UTF-8");
HttpUriRequest request = RequestBuilder.create(method)
.setConfig(requestConfig)
.setUri(url)
.setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8")
.setEntity(entity)
.build();
String req = "REQUEST:" + "\n" + request.getRequestLine() + "\n" + "Headers: " +
request.getAllHeaders() + "\n" + EntityUtils.toString(entity) + "\n";
HttpClientBuilder.create().build().withCloseable {httpClient ->
httpClient.execute(request).withCloseable {response ->
String res = "RESPONSE:" + "\n" + response.getStatusLine() + "\n" + "Headers: " +
response.getAllHeaders() + "\n" +
(response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";
System.out.println(req + "\n" + res );
return Arrays.asList(req, res);
}
}
}
List sendGet(String url, Map<String,String> body) {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(2000)
.setSocketTimeout(3000)
.build();
RequestBuilder requestBuilder = RequestBuilder.get()
.setConfig(requestConfig)
.setUri(url)
.setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
body.forEach({key, value -> requestBuilder.addParameter(key, value)});
HttpUriRequest request = requestBuilder.build();
String req = "REQUEST:" + "\n" + request.getRequestLine() + "\n" + "Headers: " +
request.getAllHeaders() + "\n";
HttpClientBuilder.create().build().withCloseable {httpClient ->
httpClient.execute(request).withCloseable {response ->
String res = "RESPONSE:" + "\n" + response.getStatusLine() + "\n" + "Headers: " +
response.getAllHeaders() + "\n" +
(response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";
System.out.println(req + "\n" + res );
return Arrays.asList(req, res);
}
}
}
The approach which is normally used in JMeter is placing your request under the While Controller which will be checking the Status value which in its turn can be fetched from the response using a suitable Post-Processor so the request will be retried unless the "Status" changes to some value which you expect (or times out)
If you place the whole construction under the Transaction Controller you will get the whole time for the status to change.
Example test plan outline:
I am trying pass contact id as a parameter to one of the webservices and get value to update in account object. But i am not able to set contact records ID field as a parameter in end point URL.
List<Contact> ContactUpdate = [SELECT id FROM Contact where Rep__c like 'CRM%'];
String ContactID;
HttpRequest req = new HttpRequest();
req.setTimeout(60000);
req.setHeader('Accept','*/*');
req.setHeader('Content-Type','application/json'); // Content Type
req.setMethod('GET');
for (Contact c : ContactUpdate)
{
ContactID = c.id;
req.setEndpoint('https://xxx/xxxx/xxxxx/xxx/xxx-lookup? ContactID= {! ContactID});
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
JSONParser parser = JSON.createParser(res.getBody());
String GMMID;
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'GCGMM')) {
// Get the value.
parser.nextToken();
// Compute the grand total price for all invoices.
GMMID = parser.gettext();
}
}
//ContactUpdate.IsFutureContext__c = true;
C.Group_ID__c = GMMID;
update c;
}
Could someone please guide me in adding variable as parameter in endpoint URL.
Try using the body and removing it from your endpoint.
String body = 'ContactID=' + contactID;
req.setbody(body);
I am uploading excel file on server through Web Api. I need to pass some parameters along with the file.
But when I am using fiddler i can able to get either Request parameters or file only, but i need both things in my web API controller.
Method Type: Post
URL : http://localhost/MP.Services/api/catalog/file/upload/
Request Header-
User-Agent: Fiddler
Host: localhost
Content-Length: 74
Content-Type: application/json; charset=utf
Request Body-
{
"CatalogCode":"1",
"Action":"1",
"Entity":"1",
"UploadedBy":"1"
}
Above is my Normal way to pass request parameter, I tried to upload the excel file in fiddler from 'Upload file' option along with above request, but when file gets uploaded request header and request body gets change.
When I run this then I will not able to get the Request Parameters to my web API controller.
My Controller Action Code-
[HttpPost]
public async Task<HttpResponseMessage> UploadCatalogExcel(CatalogUploadRequest catalogUploadRequest)
{
if (catalogUploadRequest == null)
return CreateResponse(HttpStatusCode.NotAcceptable, ControllerErrorCodeConstants.RequestIsInvalid, "Invalid request");
if (!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
if (!Request.Content.IsMimeMultipartContent())
return Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, "Uploading file is mandatory");
try
{
//Request to BL Mapping
var catalogUploadExcelBi = CatalogUploadMapping.UploadCatalogExcelRequestToBiMap(catalogUploadRequest);
var CatalogInfo = _catalogUploadBi.GetSampleCatalogExcel(catalogUploadExcelBi.CatalogCode);
string uploadPath = HttpContext.Current.Server.MapPath("~/App_Data");
uploadPath = uploadPath + "\\Upload\\" + CatalogInfo.SellerAccountId + "_" + CatalogInfo.SellerAccountName + "\\" + DateTime.Now.Year.ToString() + "_" + CatalogInfo.CatalogCode; // Physical File Location
string currentTime = Regex.Replace(DateTime.Now.ToString(), "[^0-9]+", "");
string name = catalogUploadExcelBi.CatalogCode + "_" + currentTime + ".xlsx"; // File Name
catalogUploadExcelBi.FileName = "aa";
catalogUploadExcelBi.FilePath = uploadPath;
bool exists = System.IO.Directory.Exists(uploadPath);
if (!exists)
System.IO.Directory.CreateDirectory(uploadPath);
MyStreamProvider streamProvider = new MyStreamProvider(uploadPath);
await Request.Content.ReadAsMultipartAsync(streamProvider);
var response = _catalogUploadBi.LogUploadCatalogExcel(catalogUploadExcelBi);
if (response.ServerErrors != null && response.ServerErrors.Count != 0)
{
response.ServerErrors = response.ServerErrors;
return Request.CreateResponse(HttpStatusCode.BadRequest, response.ServerErrors);
}
return Request.CreateResponse(HttpStatusCode.OK, "File uploaded successfully");
}
catch (Exception ex)
{
var error = _errorManager.GetCustomeError(ex.GetType().ToString());
return error != null && !string.IsNullOrWhiteSpace(error.ErrorCode) && !string.IsNullOrWhiteSpace(error.ErrorMessage)
? Request.CreateResponse(string.Format("Exception Occured! Error code : {0} Error Message : {1}", error.ErrorCode,
error.ErrorMessage)) : Request.CreateResponse(string.Format("Upload file method Exception Occured!"));
}
}
I hope somebody can help me, I have the following function to do remote calls to a powershell server:
private static string DoCall()
{
string resultPs = string.Empty;
string serverName = "myserver.com";
string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
System.Uri serverUri = new Uri(String.Format("http://{0}/powershell?serializationLevel=Full", serverName));
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(serverUri, SHELL_URI, (PSCredential)null);
//connectionInfo = new WSManConnectionInfo(serverUri, SHELL_URI, PSCredential.Empty);
//connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
try
{
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
PowerShell powershell = PowerShell.Create();
powershell.Runspace = runspace;
runspace.Open();
powershell.AddScript("Get-DatabaseAvailabilityGroup");
Collection<PSObject> results = powershell.Invoke();
if (powershell.Streams.Error.Count > 0)
{
foreach (ErrorRecord err in powershell.Streams.Error)
{
resultPs += (err.ErrorDetails.Message != null ? err.ErrorDetails.Message : "");
}
}
foreach (PSObject result in results)
{
foreach (PSPropertyInfo propertyInfo in result.Properties)
{
resultPs += "Property:" + propertyInfo.Name + " Value:" + propertyInfo.Value;
}
}
powershell.Runspace.Close();
}
}
catch (Exception ex)
{
resultPs = ex.Message + " " + ex.StackTrace + " innerException: " + (ex.InnerException != null ? (ex.InnerException.Message ?? "") : "");
}
return resultPs;
}
}
If I run this code within Visual Studio in a console application, my credentials are used and the exchange call is done as expected, however, if I publish this code in a WCF service and set the application pool to use my credentials I receive the following exception
"An internal error occurred. \0 at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.Initialize(Uri connectionUri, WSManConnectionInfo connectionInfo)\r\n at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid runspacePoolInstanceId, WSManConnectionInfo connectionInfo, PSRemotingCryptoHelper cryptoHelper)\r\n at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession session, PSRemotingCryptoHelper cryptoHelper, RunspaceConnectionInfo connectionInfo, URIDirectionReported uriRedirectionHandler)\r\n at System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal rsPool, URIDirectionReported uriRedirectionHandler)\r\n at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable)\r\n at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo)\r\n at System.Management.Automation.Runspaces.RunspacePool..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo)\r\n at System.Management.Automation.RemoteRunspace..ctor(TypeTable typeTable, RunspaceConnectionInfo connectionInfo, PSHost host, PSPrimitiveDictionary applicationArguments)\r\n at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments)\r\n at TestingRemote.Service.DoCall() in "
I have checked several blogs about passing the credentials but I haven't found someone with the same exact issue and that has solved mine. I know I can use an overloaded method for initializing the Credentials, but I want to use the ones in the app pool.
Currently I can send email successfully through WebDAV with C#, but there is a shortage in my App that I have hard-code the Exchange Server of my outlook, so it might only works for me, if it were moved to another PC and use another outlook account, it might not work because the Exchange Server for this outlook account might not the same as mine(that's beacuse our company for different email account might assign different Exchange server), so my question is that how can I get the Exchange Server for the current Email accout dynamically. In fact I can get this Exchange Server from the outlook client when I clicked the menu item to add a new Outlook Account, but dose there exist any API for me to get this Exchange Server programmatically such as with C#?
In fact I use the following code to send Email:
using System;
using System.Net;
using System.IO;
namespace WebDavNET
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
try
{
// TODO: Replace with the name of the computer that is running Exchange 2000.
string strServer = "ExchServe";
// TODO: Replace with the sender's alias.
string strSenderAlias = "sender";
// TODO: Replace with the sender's e-mail address.
string strFrom = "sender#example.com";
// TODO: Replace with the recipient's e-mail address.
string strTo = "recipient#example.com";
string strSubject = "Send Using HttpWebRequest";
string strBody = "Hello World";
string sUri;
sUri = "http://" + strServer + "/Exchange/" + strSenderAlias;
sUri = sUri + "/%23%23DavMailSubmissionURI%23%23/";
System.Uri myUri = new System.Uri(sUri);
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);
string sQuery;
DateTime mySentTime = new DateTime();
sQuery = "From: " + strFrom + "\n" +
"To: " + strTo + "\n" +
"Subject: " + strSubject + "\n" +
"Date: " + DateTime.Now.ToString() + "\n" +
"X-Mailer: My DAV mailer" + "\n" +
"MIME-Version: 1.0" + "\n" +
"Content-Type: text/plain;" + "\n" +
"Charset = \"iso-8859-1\"" + "\n" +
"Content-Transfer-Encoding: 7bit" + "\n" + "\n" +
strBody;
// Set the credentials.
// TODO: Replace with the appropriate user credential.
NetworkCredential myCred = new NetworkCredential(#"DomainName\User", "Password");
CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myCred);
HttpWRequest.Credentials = myCredentialCache;
// Set the headers.
HttpWRequest.Headers.Set("Translate", "f");
HttpWRequest.ContentType = "message/rfc822";
HttpWRequest.ContentLength = sQuery.Length;
//Set the request timeout to 5 minutes.
HttpWRequest.Timeout = 300000;
// Set the request method.
HttpWRequest.Method = "PUT";
// Store the data in a byte array.
byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery);
HttpWRequest.ContentLength = ByteQuery.Length;
Stream QueryStream = HttpWRequest.GetRequestStream();
// write the data to be posted to the Request Stream
QueryStream.Write(ByteQuery,0,ByteQuery.Length);
QueryStream.Close();
// Send the request and get the response.
HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
// Get the Status code.
int iStatCode = (int)HttpWResponse.StatusCode;
string sStatus = iStatCode.ToString();
Console.WriteLine("Status Code: {0}", sStatus);
// Get the request headers.
string sReqHeaders = HttpWRequest.Headers.ToString();
Console.WriteLine(sReqHeaders);
// Read the response stream.
Stream strm = HttpWResponse.GetResponseStream();
StreamReader sr = new StreamReader(strm);
string sText = sr.ReadToEnd();
Console.WriteLine("Response: {0}", sText);
// Close the stream.
strm.Close();
// Clean up.
myCred = null;
myCredentialCache = null;
HttpWRequest = null;
HttpWResponse = null;
QueryStream = null;
strm = null;
sr = null;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
}
As you can see in the code there is a variable named "strServer", In my App I just hard-code my Exchange Server for this variable, so my question is that dose there exist any API for me to get the Exchange Server dynamically for the specific outlook account?
Thanks!
You can use EWS(exchange Web Services) too. here is the link
You can use XML creator for creating items or requests required for operations in the link. Just go through the operations given on the link.