How to create a Body for this Multipart POST request - http

I ran a API request thorough postman and it ran successfully and this is its HTTP
POST //dummyendpoint/ HTTP/1.1
Host: dummyhost.com
Authorization: token="dummy_token"
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="config_enroll_settings"; filename="/C:/Users/SaurabhKumar/Desktop/config_enroll_settings.xml"
Content-Type: text/xml
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="usagePolicy"; filename="/C:/Users/SaurabhKumar/Desktop/usagePolicy.xml"
Content-Type: text/xml
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
But then I am trying to achieve the same through JAXRS implementation I am getting 500 response code and when I checked the server side logs it said couldnt determine MIME boundary
This is my JAXRS interface I have explicitly added boundary but still it dosent works
#Consumes("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
#POST
#Path("/dummy_endpoint/{billingId}/")
public Object configureDeviceEnrollSettings(
#PathParam("billingId") String billingId,
#Multipart("multipartBody") MultipartBody multipartBody,
#HeaderParam("Authorization") String authorizationHeaderValue);
This is how I am calling this interface
Attachment configSettingsFile = new Attachment("config_enroll_settings", "multipart/form-data", new File("/C:/Users/SaurabhKumar/Desktop/config_enroll_settings.xml"));
Attachment usagePolicyFile = new Attachment("usagePolicy", "multipart/form-data", new File("/C:/Users/SaurabhKumar/Desktop/usagePolicy.xml"))
/*
I have tried this way also This way it resulted in exception
No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain:
org.apache.cxf.jaxrs.ext.multipart.MultipartBody["childAttachments"]
->java.util.AbstractList$SubList[0]
->org.apache.cxf.jaxrs.ext.multipart.Attachment["dataHandler"]
->javax.activation.DataHandler["dataSource"]
->org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource["inputStream"]
->java.io.FileInputStream["fd"])
File file1 = new File("/C:/Users/SaurabhKumar/Desktop/config_enroll_settings.xml");
ContentDisposition cd1 = new ContentDisposition("form-data; name=\"config_enroll_settings\"; filename=\"/C:/Users/SaurabhKumar/Desktop/config_enroll_settings.xml\"");
Attachment configSettingsFile = new Attachment("config_enroll_settings", new FileInputStream(file1), cd1);
File file2 =new File("/C:/Users/SaurabhKumar/Desktop/usagePolicy.xml");
ContentDisposition cd2 = new ContentDisposition("form-data; name=\"config_enroll_settings\"; filename=\"/C:/Users/SaurabhKumar/Desktop/usagePolicy.xml\"");
Attachment usagePolicyFile = new Attachment("usagePolicy", new FileInputStream(file2), cd2);
*/
List<Attachment> list=new LinkedList<>();
list.add(configSettingsFile);
list.add(usagePolicyFile);
MultipartBody body = new MultipartBody(list);
Object object = accountResource.configureDeviceEnrollSettings(billingId, body, "token=" + "\"" + authToken + "\"");
here is the stackTrace
com.webservices.utilities.httpclient.http.rest.ApiException: status 500 reading AccountResourceClient#configureDeviceEnrollSettings(String,MultipartBody,String)
at com.webservices.utilities.httpclient.http.rest.ApiErrorDecoder.decode(ApiErrorDecoder.java:42)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:149)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
at com.sun.proxy.$Proxy16.configureDeviceEnrollSettings(Unknown Source)
at com.webservices.settings.ImportSettingsRoutine.importConfig(ImportSettingsRoutine.java:103)
at com.webservices.abstractclasses.AbstractRoutine.executeRoutine(AbstractRoutine.java:14)
at com.webservices.clients.ImportClient.executeRoutineQueue(ImportClient.java:47)
at com.webservices.Main.main(Main.java:69)
here is the raw request from the jaxrs client
POST https:/dummyendpoint/ HTTP/1.1
Accept: application/xml
Authorization: token="dummy_token"
Content-Length: 1861
Content-Type: multipart/form-data
User-Agent: ApiClient-1.0
X-FL-APPNAME: ApiClient
X-FL-REQ-ID: dumm_id
{
"type" : {
"type" : "multipart",
"subtype" : "related",
"parameters" : { },
"wildcardType" : false,
"wildcardSubtype" : false
},
"rootAttachment" : {
"headers" : {
"Content-ID" : [ "config_enroll_settings" ],
"Content-Type" : [ "text/xml" ]
},
"object" : "C:\\Users\\SaurabhKumar\\Desktop\\config_enroll_settings.xml",
"contentType" : {
"type" : "text",
"subtype" : "xml",
"parameters" : { },
"wildcardType" : false,
"wildcardSubtype" : false
},
"contentId" : "config_enroll_settings"
},
"allAttachments" : [ {
"headers" : {
"Content-ID" : [ "config_enroll_settings" ],
"Content-Type" : [ "text/xml" ]
},
"object" : "C:\\Users\\SaurabhKumar\\Desktop\\config_enroll_settings.xml",
"contentType" : {
"type" : "text",
"subtype" : "xml",
"parameters" : { },
"wildcardType" : false,
"wildcardSubtype" : false
},
"contentId" : "config_enroll_settings"
}, {
"headers" : {
"Content-ID" : [ "usagePolicy" ],
"Content-Type" : [ "text/xml" ]
},
"object" : "C:\\Users\\SaurabhKumar\\Desktop\\usagePolicy.xml",
"contentType" : {
"type" : "text",
"subtype" : "xml",
"parameters" : { },
"wildcardType" : false,
"wildcardSubtype" : false
},
"contentId" : "usagePolicy"
} ],
"childAttachments" : [ {
"headers" : {
"Content-ID" : [ "usagePolicy" ],
"Content-Type" : [ "text/xml" ]
},
"object" : "C:\\Users\\SaurabhKumar\\Desktop\\usagePolicy.xml",
"contentType" : {
"type" : "text",
"subtype" : "xml",
"parameters" : { },
"wildcardType" : false,
"wildcardSubtype" : false
},
"contentId" : "usagePolicy"
} ]
}

Related

Wiremock matchesJSONPath if null or empty

I'm trying to add a Wiremock stub that matches if the JSON in a request body is either non-existent OR an empty string.
The stub I have at the moment is:
{
"id" : "e331007e-3e6d-4660-b575-b04e774e88c6",
"request" : {
"urlPathPattern" : "/premises/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/bookings/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/non-arrivals",
"method" : "POST",
"bodyPatterns" : [ {
"matchesJsonPath" : "$.[?(#.reason === '' || #.reason == null)]"
} ]
},
"response" : {
"status" : 400,
"jsonBody" : {
"type" : "https://example.net/validation-error",
"title" : "Invalid request parameters",
"code" : 400,
"invalid-params" : [ {
"propertyName" : "reason",
"errorType" : "blank"
} ]
},
"headers" : {
"Content-Type" : "application/problem+json;charset=UTF-8"
}
},
"uuid" : "e331007e-3e6d-4660-b575-b04e774e88c6"
}
It matches is the reason is '', but not if reason is not present. Any ideas?

How do I filter out some logs?

Because some assemblies have noisy logs and I only want to get warning, error, and fatal log messages.
Here is my code, what is the problem?
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.CreateBootstrapLogger();
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((context, services, cfg) => cfg
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.MinimumLevel.Information()
.Enrich.WithProperty("Version", Assembly.GetEntryAssembly()?.GetName().Version?.ToString())
.Enrich.FromLogContext()
.WriteTo.Logger(lc =>
{
lc.WriteTo.Console();
})
.WriteTo.Logger(lc =>
{
lc.Filter.ByExcluding(x =>
x.Properties.ContainsKey("SourceContext") &&
x.Properties["SourceContext"].ToString() == "Marvin.Cache.Headers.HttpCacheHeadersMiddleware" &&
x.Level is LogEventLevel.Verbose or LogEventLevel.Debug or LogEventLevel.Information)
.WriteTo.MongoDBBson(mongoDbCfg =>
{
// db option...
});
}));
But I still have Marvin.Cache.Headers.HttpCacheHeadersMiddleware Information logs in my database
log
{
"_id" : ObjectId("61c981a91ba9664afac0778c"),
"Level" : "Information",
"UtcTimeStamp" : ISODate("2021-12-27T09:04:40.674Z"),
"MessageTemplate" : {
"Text" : "Vary header generated: Accept, Accept-Language, Accept-Encoding.",
"Tokens" : [
{
"_t" : "TextToken",
"StartIndex" : 0,
"Text" : "Vary header generated: Accept, Accept-Language, Accept-Encoding."
}
]
},
"RenderedMessage" : "Vary header generated: Accept, Accept-Language, Accept-Encoding.",
"Properties" : {
"SourceContext" : "Marvin.Cache.Headers.HttpCacheHeadersMiddleware",
"RequestId" : "0HME92VII1HBR:00000007",
"RequestPath" : "/api/Audio",
"ConnectionId" : "0HME92VII1HBR",
"Version" : "2.0.5.8031"
},
"Exception" : {
"_csharpnull" : true
}
}
Through my debugging in the past few days, I found that the value of x.Properties["SourceContext"].ToString() has quotation marks, which is not an easy problem to find.
So my updated code looks like this.
Func<LogEvent, bool> logFilter = x => x.Properties.ContainsKey("SourceContext") &&
x.Properties["SourceContext"] is ScalarValue
{
Value: "Marvin.Cache.Headers.HttpCacheHeadersMiddleware"
} &&
(x.Level is LogEventLevel.Verbose or LogEventLevel.Debug
or LogEventLevel.Information);
lc.Filter.ByExcluding(logFilter);

Spring-cloud-contract stubrunner test run failing due to 404

I am getting the following error when I run my client application using stubrunner
Getting org.springframework.web.client.HttpClientErrorException: 404
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
wiremock is giving the following information
{
"url" : "/app/crm/customer/40",
"absoluteUrl" : "http://localhost:6565/app/crm/customer/40",
"method" : "GET",
"clientIp" : "127.0.0.1",
"headers" : {
"accept" : "text/plain, text/plain, application/json, application/json, application/*+json, application/*+json, */*, */*",
"domain" : "IND",
"host" : "localhost:6565",
"connection" : "Keep-Alive",
"user-agent" : "Apache-HttpClient/4.5.3 (Java/1.8.0_144)",
"accept-encoding" : "gzip,deflate"
},
"cookies" : { },
"browserProxyRequest" : false,
"loggedDate" : 1508757427831,
"bodyAsBase64" : "",
"body" : "",
"loggedDateString" : "2017-10-23T11:17:07Z"
}
Closest match:
{
"urlPattern" : "/app/crm/customer/[0-9]{2}",
"method" : "GET",
"headers" : {
"Content-Type" : {
"equalTo" : "application/json"
},
"domain" : {
"equalTo" : "IND"
}
}
}
My Contract looks like this
package contracts
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method 'GET'
url value(consumer(regex('/app/crm/customer/[0-9]{2}')), producer('/app/crm/customer/40'))
headers {
contentType(applicationJson())
}
}
response {
status 200
headers {
contentType(applicationJson())
}
}
}
I tried to resolve the issue by changing content types and other details.where am I doing mistake. Thanks.
It's written exactly what the problem is. In other words the answer is there in your question.
To match your request it needs to be looking like this
{
"urlPattern" : "/app/crm/customer/[0-9]{2}",
"method" : "GET",
"headers" : {
"Content-Type" : {
"equalTo" : "application/json"
},
"domain" : {
"equalTo" : "IND"
}
}
}
These are the headers that you are passing
"headers" : {
"accept" : "text/plain, text/plain, application/json, application/json, application/*+json, application/*+json, */*, */*",
"domain" : "IND",
"host" : "localhost:6565",
"connection" : "Keep-Alive",
"user-agent" : "Apache-HttpClient/4.5.3 (Java/1.8.0_144)",
"accept-encoding" : "gzip,deflate"
},
I don't see the Content-Type equal to application/json header. That's why the request is not matched.

HTTPOISON - insert body parameters in elixir

I'm trying to do a http request
def getPage() do
url = "http://myurl"
body = '{
"call": "MyCall",
"app_key": "3347249693",
"param": [
{
"page" : 1,
"registres" : 100,
"filter" : "N"
}
]
}'
headers = [{"Content-type", "application/json"}]
HTTPoison.post(url, body, headers, [])
end
this works for me well.
my question is - how can I insert variables in the body request.
meaning:
def getPage(key, page, registers, filter) do
url = "http://myurl"
body = '{
"call": "MyCall",
"app_key": key,
"param": [
{
"page" : page,
"registres" : registers,
"filter" : filter
}
]
}'
headers = [{"Content-type", "application/json"}]
HTTPoison.post(url, body, headers, [])
end
when I run it I get
%HTTPoison.Response{body: "\nFatal error: Uncaught exception 'Exception' with message 'Invalid JSON object' in /myurl/www/myurl_app/api/lib/php-wsdl/class.phpwsdl.servers.php:...
any suggestions?
You really should be using a JSON encoder like Poison for this.
url = "http://myurl"
body = Poison.encode!(%{
"call": "MyCall",
"app_key": key,
"param": [
%{
"page": page,
"registres": registers,
"filter": filter
}
]
})
headers = [{"Content-type", "application/json"}]
HTTPoison.post(url, body, headers, [])
You need to interpolate the values:
body = '{
"call": "MyCall",
"app_key": "#{key}",
"param": [
{
"page" : #{page},
"registres" : "#{registres}",
"filter" : "#{filter}"
}
]
}'
If you use a JSON library (Poison is a popular choice) Then you could do something like this to turn Elixir data structures into a JSON representation:
body = %{
call: "MyCall",
app_key: key,
param: [
{
page: page,
registres: registers,
filter: filter
}
]
} |> Poison.encode!()

problems on elasticsearch with parent child documents

We work with two types of documents on elastic search (ES): items and slots, where items are parents of slot documents.
We define the index with the following command:
curl -XPOST 'localhost:9200/items' -d #itemsdef.json
where itemsdef.json has the following definition
{
"mappings" : {
"item" : {
"properties" : {
"id" : {"type" : "long" },
"name" : {
"type" : "string",
"_analyzer" : "textIndexAnalyzer"
},
"location" : {"type" : "geo_point" },
}
}
},
"settings" : {
"analysis" : {
"analyzer" : {
"activityIndexAnalyzer" : {
"alias" : ["activityQueryAnalyzer"],
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["trim", "lowercase", "asciifolding", "spanish_stop", "spanish_synonym"]
},
"textIndexAnalyzer" : {
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["word_delimiter_impl", "trim", "lowercase", "asciifolding", "spanish_stop", "spanish_synonym"]
},
"textQueryAnalyzer" : {
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["trim", "lowercase", "asciifolding", "spanish_stop"]
}
},
"filter" : {
"spanish_stop" : {
"type" : "stop",
"ignore_case" : true,
"enable_position_increments" : true,
"stopwords_path" : "analysis/spanish-stopwords.txt"
},
"spanish_synonym" : {
"type" : "synonym",
"synonyms_path" : "analysis/spanish-synonyms.txt"
},
"word_delimiter_impl" : {
"type" : "word_delimiter",
"generate_word_parts" : true,
"generate_number_parts" : true,
"catenate_words" : true,
"catenate_numbers" : true,
"split_on_case_change" : false
}
}
}
}
}
Then we add the child document definition using the following command:
curl -XPOST 'localhost:9200/items/slot/_mapping' -d #slotsdef.json
Where slotsdef.json has the following definition:
{
"slot" : {
"_parent" : {"type" : "item"},
"_routing" : {
"required" : true,
"path" : "parent_id"
},
"properties": {
"id" : { "type" : "long" },
"parent_id" : { "type" : "long" },
"activity" : {
"type" : "string",
"_analyzer" : "activityIndexAnalyzer"
},
"day" : { "type" : "integer" },
"start" : { "type" : "integer" },
"end" : { "type" : "integer" }
}
}
}
Finally we perform a bulk index with the following command:
curl -XPOST 'localhost:9200/items/_bulk' --data-binary #testbulk.json
Where testbulk.json holds the following data:
{"index":{"_type": "item", "_id":35}}
{"location":[40.4,-3.6],"id":35,"name":"A Name"}
{"index":{"_type":"slot","_id":126,"_parent":35}}
{"id":126,"start":1330,"day":1,"end":1730,"activity":"An Activity","parent_id":35}
We see through ES Head plugin that definitions seem to be ok. We test the analyzers to check that they have been loaded and they work. Both documents appear listed in ES Head browser view. But if we try to retrieve the child item using the API, ES responds that it does not exist:
$ curl -XGET 'localhost:9200/items/slot/126'
{"_index":"items","_type":"slot","_id":"126","exists":false}
When we import 50 documents, all parent documents can be retrieved through API, but only SOME of the requests for child elements get a successful response.
My guess is that it may have something to do with how docs are stored across shards and the routing...which certainly is not clear to me how it works.
Any clue on how to be able to retrieve individual child documents? ES Head shows they have been stored but HTTP GETs to localhost:9200/items/slot/XXX respond randomly with "exists":false.
The child documents are using parent's id for routing. So, in order to retrieve child documents you need to specify parent id in the routing parameter on your query:
curl "localhost:9200/items/slot/126?routing=35"
If parent id is not available, you will have to search for the child documents:
curl "localhost:9200/items/slot/_search?q=id:126"
or switch to an index with a single shard.

Resources