C# reading OData $batch multipart/mixed response to a meaningful object(s) - odata

I am consuming an OData Service, I am successfully POSTing my request (using RestSharp) to /$batch endpoint and getting the response. the response header contains
"Content-Type" : "multipart/mixed; boundary=<GUID>"
Body is
--C4254E82B51CFE5BD04201606B9AB7C50
Content-Type: multipart/mixed; boundary=C4254E82B51CFE5BD04201606B9AB7C51
Content-Length: 2221
--C4254E82B51CFE5BD04201606B9AB7C51
Content-Type: application/http; charset=utf-8
Content-Length: 2037
content-transfer-encoding: binary
HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 1732
location: https://test.api/Event/CarEntries('4003581738')
dataserviceversion: 2.0
etag: W/"datetimeoffset'2021-04-21T00%3A49%3A45Z'"
{ JSON }
--C4254E82B51CFE5BD04201606B9AB7C51--
--C4254E82B51CFE5BD04201606B9AB7C50
Content-Type: application/http; charset=utf-8
Content-Length: 15116
content-transfer-encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 15017
dataserviceversion: 2.0
{ JSON }
--C4254E82B51CFE5BD04201606B9AB7C50--
How do I deserialise and extract the JSON Objects in my C# code? I do not want to invent a Regex pattern (well that is my last resort)
I did try using "Simple.OData.Client" (also a few others) but my request is not 100% compatible with the "Simple.OData.Client".
Also tried extracting using the below code but not necessary give me what I want
var sc = new StringContent(response.Content);
var content = sc.ReadAsStreamAsync().Result;
var streamContent = new StreamContent(content);
streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse(response.ContentType);
var provider = streamContent.ReadAsMultipartAsync().Result;
Can someone giveme the best way to extract the Json objects ?
Thanks
Nero

I manage to get this working HttpClient and System.Net.Http.Formatting.Extension
Below is the code
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://service-url/Entity/v1.3/$batch"))
{
request.Headers.TryAddWithoutValidation("Accept", "application/json");
request.Headers.TryAddWithoutValidation("Client-Id", "XXXXXXXXX");
// Add all the headers here
// this is your custom batch request
request.Content = new StringContent("--batch\ncontent-type: multipart/mixed;boundary=changeset\n\n--changeset\ncontent-type: application/http\nContent-Transfer-Encoding: binary\n\nPOST CarEntries HTTP/1.1\ncontent-type: application/json;charset=utf-8\naccept: application/json;\n\n{\n\"RefId\": \"Test\",\n\"Child\": {\n\"ChildId\": \"412000415\"\n}\n}\n--changeset--\n--batch--");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/mixed;boundary=batch"); // This is imporatnt - but please refer to your api documentation
var response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var multiPartContent = await response.Content.ReadAsMultipartAsync(); // This is part of the extension
var mixedContent = multiPartContent.Contents.First(); // you will have multiple contents, select the content you want
var data = await mixedContent.ReadAsStringAsync();// read it as string
Regex rg = new Regex(#"\{(.|\s)*\}"); // find the json object from mixed content
var json = rg.Match(data);
return JsonConvert.DeserializeObject<TMessage>(json.Value);
}
}
Hope this helps someone in the future. But still, my goal is to use "Simple.OData.Client" or "Microsoft.OData.Client"

Related

Send MultiValueMap as MultiPartFormData in Feign Client

I am trying to convert the below kotlin code from RestTemplate to Feign client. The rest template code sends multiValueMap as request with content-type header multipart/form-data and consumes JSON object as response.
RestTemplate Code:
var headers = HttpHeaders()
headers.contentType = MediaType.MULTIPART_FORM_DATA
headers.add("custom-header", "value")
val body: MultiValueMap<String, Any> = LinkedMultiValueMap()
body.add("field1", "value1")
body.add("field2", "value2")
val requestEntity = HttpEntity(body, headers)
return restTemplate.postForEntity("https://enmf7tx8y37x.x.pipedream.net/", requestEntity, Object::class.java)
In this case the request is sent as below:
Headers:
Host: enmf7tx8y37x.x.pipedream.net
X-Amzn-Trace-Id: Root=1-6303ecb2-19a833a044ab3bf83f74f256
Content-Length: 342
Accept: application/xml, text/xml, application/json, application/*+xml, application/*+json
Content-Type: multipart/form-data;boundary=_MtEGFIF4XK_aOU8QsXstQuCliV1-llj
custom-header: value
X-B3-TraceId: a67561ec329f9a16
X-B3-SpanId: a6cc94e403bfe318
X-B3-ParentSpanId: a67561ec329f9a16
X-B3-Sampled: 1
User-Agent: Apache-HttpClient/4.5.13 (Java/17.0.3)
Accept-Encoding: gzip,deflate
Body:
--_MtEGFIF4XK_aOU8QsXstQuCliV1-llj
Content-Disposition: form-data; name="field1"
Content-Type: text/plain;charset=UTF-8
Content-Length: 6
value1
--_MtEGFIF4XK_aOU8QsXstQuCliV1-llj
Content-Disposition: form-data; name="field2"
Content-Type: text/plain;charset=UTF-8
Content-Length: 6
value2
--_MtEGFIF4XK_aOU8QsXstQuCliV1-llj--
I tried to do the same in Feign client:
code:
/*val headers = HttpHeaders()
headers.contentType = MediaType.MULTIPART_FORM_DATA
headers.add("custom-header", "value")*/
val body: MultiValueMap<String, Any> = LinkedMultiValueMap()
body.add("field1", "value1")
body.add("field2", "value2")
val result = testClient.test("value", body)
Feign Client:
#FeignClient(
value = "testClient",
url = "https://enmf7tx8y37x.x.pipedream.net/"
)
interface TestClient {
#PostMapping(
consumes = [MediaType.MULTIPART_FORM_DATA_VALUE],
produces = [MediaType.APPLICATION_JSON_VALUE]
)
fun test(
#RequestHeader(value = "custom-header") customHeader: String,
#RequestPart("request") request: MultiValueMap<String, Any>
): ResponseEntity<Object>
}
The header are fine but no value present in the body.
Header:
Host: enmf7tx8y37x.x.pipedream.net
X-Amzn-Trace-Id: Root=1-6303ef0f-78c869881a5b27d0707eab9e
Content-Length: 17
Accept: application/json
Authorization: Basic aHlwb2xhYjp0ZXN0c211cmY=
Content-Type: multipart/form-data; charset=UTF-8; boundary=182c75dd399
custom-header: value
X-B3-TraceId: 2989eb4f12e3d417
X-B3-SpanId: 23414bcdf365784c
X-B3-ParentSpanId: 2989eb4f12e3d417
X-B3-Sampled: 1
User-Agent: Java/17.0.3
Body:
--182c75dd399--
I had to add consumes value as multipart/form-data instead of json to get the right header values for Accept and Content-Type.
How can I populate the request using Feign client? If the #RequestPart is String then the value is sent in the body but any other data type like multiValueMap, byteArray, etc were not working
In feign client you cannot use MultiValueMap directly. You have to use MultipartFile datatype for bytearray and for the remaining metadata fields you need to mention each one as a separate argument in the method. Then FeignClient will generate the same request like the one you showed when using RestTemplate.
import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.multipart.MultipartFile
#FeignClient(
value = "testClient",
url = "https://enmf7tx8y37x.x.pipedream.net/"
)
interface TestClient {
#PostMapping(
consumes = [MediaType.MULTIPART_FORM_DATA_VALUE],
produces = [MediaType.APPLICATION_JSON_VALUE]
)
fun test(
#RequestHeader(value = "custom-header") customHeader: String,
#RequestPart(name = "file") file: MultipartFile,
#RequestPart(name = "field1") field1: String
): ResponseEntity<Object>
}
code sample for How to create MultiPartFile:
import org.springframework.mock.web.MockMultipartFile
//val multipartFile: MultipartFile = MockMultipartFile("filename", byteArray)
val multipartFile: MultipartFile = MockMultipartFile("filename", "filename", "content type like application/pdf", byteArray)

Getting a 401 unauthorized error on One Login authentication grant type part 2 request

I am using MVC framework to make POST request to OneLogin API to get JWT. I am getting a 401 unauthorized message back at my PostAsync call in the code below.
Error look like following
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
Cache-Control: no-store, no-cache
Date: Wed, 17 Jun 2020 04:21:23 GMT
Set-Cookie: ol_oidc_canary_30=false; path=/; domain=.onelogin.com
X-Powered-By: Express
Content-Length: 77
Content-Type: application/json; charset=utf-8
}
Am i missing any parameters. I registered my localhost on One login dev account. Is there any setting there i need to update or change?
public async Task<OidcTokenResponse> ProcessToken(string code, string clientSec)
{
string authorityToken = OneLoginAuthorityToken;
var formData = new System.Net.Http.FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("code", code),
new KeyValuePair<string, string>("client_id", OneLoginClientID),
new KeyValuePair<string, string>("client_secret", clientSec),
new KeyValuePair<string, string>("grant_type", "authorization_code"),
});
using (var client = new System.Net.Http.HttpClient())
{
const SslProtocols _Tls12 = (SslProtocols)0x00000C00;
const System.Net.SecurityProtocolType Tls12 = (System.Net.SecurityProtocolType)_Tls12;
System.Net.ServicePointManager.SecurityProtocol = Tls12;
var res = await client.PostAsync(authorityToken, formData);
var json = await res.Content.ReadAsStringAsync();
var tokenReponse = Newtonsoft.Json.JsonConvert.DeserializeObject<OidcTokenResponse>(json);
return tokenReponse;
}
}
You need to send the redirect_uri again in this message - it is a security feature of the authorization code flow.
Also worth tracing the messages with a tool such as Fiddler to ensure that the messages sent over the wire are what you'd expect.
See steps 4 and 8 of my messages write up for something to compare against.

How to view content delivered from HttpResponseMessage (ASP.NET MVC Web Api)

I create json object and assign it to a StringContent of my HttpResponseMessage instance. Everything works fine when I call the Web API action, the result is 200, the content-length is how it should be, but how to find the content itself, where is the json? What I get in the browser and in Postman is this:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: application/json
}
Why is this instead of my json string?
Content: System.Net.Http.StringContent
If what you are trying to achieve is to return a valid JSON response, then this is the way to go in Asp.Net MVC
public ActionResult HttpResponseMessage()
{
var oJSON = new { url = "path_to_file", hash = "aaaaaaaaaaaaaaaaa" };
return Json(oJSON, JsonRequestBehavior.AllowGet);
}
Response headers as seen by Postman:
Cache-Control →private
Content-Length →49
Content-Type →application/json; charset=utf-8
Date →Fri, 26 Oct 2018 13:31:44 GMT
Server →Microsoft-IIS/10.0
X-AspNet-Version →4.0.30319
X-AspNetMvc-Version →5.2
X-Powered-By →ASP.NET
X-SourceFiles →=?UTF-8?B?RTpcRXhhbSA3MCA0ODdcNzA0ODdcTVZDUm91dGVzXEhvbWVcSHR0cFJlc3BvbnNlTWVzc2FnZQ==?=
Response body as seen by Postman
{"url":"path_to_file","hash":"aaaaaaaaaaaaaaaaa"}

UCWA 2.0 Trying to add forward contact but getting ParameterValidationFailure

I am using UCWA 2.0 for some Skype4Business integration in our intranet web application.
I can successfully connect and do some stuff for example: changing availability status or asking for callforwarding settings of the user.
What I like to do that does not work is set a forward contact for the user.
Documentation for this: click here
My code for the POST call:
using (var client = new HttpClient())
{
var param = new { target = "sip:user#domain.be" };
var paramJson = JsonConvert.SerializeObject(param, Formatting.Indented);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Referrer = new Uri(result4._links.xframe.href);
client.DefaultRequestHeaders.Add("Authorization", $"{result3.token_type} {result3.access_token}");
var url = $"https://ucwebext.domain.be/{applicationUrl}/me/callForwardingSettings/immediateForwardSettings/immediateForwardToContact";
var responseNew = await client.PostAsync(url, new StringContent(paramJson, Encoding.UTF8, "application/json"));
var responseString = await responseNew.Content.ReadAsStringAsync();
}
Same code using RestSharp:
var client = new RestClient($"https://ucwebext.domain.be/{applicationUrl}");
var restR = new RestRequest("/me/callForwardingSettings/immediateForwardSettings/immediateForwardToContact", Method.POST);
var param = new { target = "sip:user#domain.be" };
var jsonToSend = JsonConvert.SerializeObject(param, Formatting.Indented);
restR.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody);
restR.AddParameter("referer", result4._links.xframe.href);
restR.AddHeader("Authorization", $"{result3.token_type} {result3.access_token}");
restR.RequestFormat = DataFormat.Json;
var response = client.ExecuteAsPost(restR, "POST");
Request header from the call:
POST https://ucwebext.domain.be/ucwa/oauth/v1/applications/101024253230/me/callForwardingSettings/immediateForwardSettings/immediateForwardToContact HTTP/1.1
Accept: application/json
Referer: https://ucwebext.domain.be/Autodiscover/XFrame/XFrame.html
Authorization: Bearer cwt=AAEBHAEFAAAAAAAF...0Q_OXzsR4g4F-PpYaMGK10Pg
Content-Type: application/json; charset=utf-8
Host: ucwebext.domain.be
Content-Length: 48
Expect: 100-continue
{"target":"sip:user#domain.be"}
I get the following error:
{"code":"BadRequest","subcode":"ParameterValidationFailure","message":"Please check what you entered and try again.","parameters":[{"name":"target","reason":"MissingOrInvalid"}]}
I tried everything and don't understand why the parameter is not submitting or invalid.
Any help is much appreciated!
SOLUTION
Instead of using JSON I added the parameter as querystring which worked!
var client = new RestClient($"https://ucwebext.domain.be/{applicationUrl}");
var restR = new RestRequest($"/me/callForwardingSettings/immediateForwardSettings/immediateForwardToContact{Url.Encode("?target=sip:user#domain.be")}", Method.POST);
restR.AddParameter("referer", result4._links.xframe.href);
restR.AddHeader("Authorization", $"{result3.token_type} {result3.access_token}");
var response = client.ExecuteAsPost(restR, "POST");

MVC 4 WebRequest WebResponse caching

I am trying to disable the caching on on the the pages. The reason - the page in question should display up to date data on each request and the data is coming from external XML feed.
I am using standard HttpWebRequest HttpWebResponse. All is working fine but I am getting some (I believe) caching issues where by querying XML feed URL directly I am getting more up to date data compared to my Controller/View data which uses the same URL.
XML feed URL is appended with random numbers on each request and Caching was disabled in the controller ([OutputCache(NoStore = true, Duration = 0)]) as well as in the ActionResult, which produces the following:
Response
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: text/html; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.1
X-AspNet-Version: 4.0.30319
X-Frame-Options: SAMEORIGIN
X-Powered-By: ASP.NET
X-UA-Compatible: IE=edge
Date: Thu, 04 Aug 2016 13:22:33 GMT
Content-Length: 75285
Request
pretty print
GET /investor-relations/share-price HTTP/1.1
Host: cms.crestnicholson-dev.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: ecos.dt=1470316952533; ASP.NET_SessionId=5lbh4v20kac0boadibjfwedr; IsAgreeToCookiePolicy=true
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
And still outdated/cached data is rendered on the page…
If someone has any ideas/suggestions, It would be greatly appreciated.
Controller code:
[OutputCache(NoStore = true, Duration = 0)]
public class InvestorRelationsController : Controller
{
public ActionResult SharePrice()
{
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
return PartialView(GetSharePrice());
}
private SharePriceModel GetSharePrice()
{
try
{
var xml = GetSharePriceXml();
return ParseSharePrice(xml);
}
catch (Exception)
{
return new SharePriceModel() {IsServiceUnavailable = true};
}
}
private SharePriceModel ParseSharePrice(string xml)
{
Guard.ArgumentNotNull(xml, "xml");
var model = new SharePriceModel();
var doc = new XmlDocument();
doc.Load(new StringReader(xml));
var root = doc.DocumentElement;
model.DateLastUpdated = root.SelectSingleNode("Time").InnerText;
model.Price = root.SelectSingleNode("CurrentPrice").InnerText;
model.Change = root.SelectSingleNode("Change").InnerText;
model.ChangePersentage = root.SelectSingleNode("PercentageChange").InnerText;
return model;
}
private string GetSharePriceXml()
{
Uri address = new Uri(SiteConfiguration.SharePriceFeedUrl);
Random random = new Random();
string url = address + "?random=" + random.Next();
// Set a default policy level for the "http:" and "https" schemes.
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
HttpWebRequest.DefaultCachePolicy = policy;
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
// Create the web request
var request = WebRequest.Create(url) as HttpWebRequest;
// Set type to POST
request.CachePolicy = noCachePolicy;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("Cache-Control", "no-cache");
request.Headers.Add("Cache-Control", "private");
// Get response
using (var response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
using (var reader = new StreamReader(response.GetResponseStream()))
{
// Console application output
return reader.ReadToEnd().Trim();
}
}
}
}

Resources