Web API response xml language - c#

I am having this simple POST url to which my clients will send a POST request.
http://mydomain.com/requests/request
POST Data:
<Request>
<OrderId>1E008921</OrderId>
<OrderName>MC1</OrderName>
</Request>
to which I respond by sending back them a XML
<SRequest>
<RequestedBy>Client 1 </RequestedBy>
<RequestName>Test Name</RequestName>
<RequestStatus>Success</RequestStatus>
<SRequest>
Now client wants the above XML in a different language (say French)
How to handle this ? How to send the response language preference in Web API ? specially with the POST scenario like this ? (Append a language string ? or what is the best practices ?)

I suggest you either include it in URL: http://mydomain.com/fr/requests/request or use HTTP header like Accept-Language. You could read more in the answer to: Good way to changing language resources dynamically by request

Related

C# Multipart/Related Parsing and Attachments

I am getting a post of Content-type Multipart/Related with an xml body with mainly PDF file attachments.
Is there a C# assembly that will handle the parsing of a post like this, where I can pull the attachments out with the purpose of saving them.
I have it done for Multipart/PostData, but it has to be for Multipart/Related.
Thanks
When you say "post", I get the feeling you are talking about an HTTP POST request?
Normally, for multipart/related (which tends to be email related), I would recommend my MimeKit library.
You could still use my MimeKit library to parse HTTP POST requests since MIME is MIME, but you could also take a look at Microsoft.AspNetCore.WebUtilities which has a MultipartReader class that will likely work great for this.

Read SOAP XML message sent as HTTP message body

I have a 3rd party that is sending SOAP XML messages, as the HTTP message body (using HTTP Post). I need to write a program to accept / process these messages.
I have a specification document which contains 2 WSDL definitions - GetTransaction and CutOff.
I am trying to use Postman to send a test message. Content-Type is set to application/xml and the body is set to raw / XML.
I am using C#, ASP.Net (4.7.2), the code is in a Class Library. I have a Controller (ApiController) with:
[HttpPost]
[ValidationAttributes.RequireHttps]
public HttpResponseMessage Service(XmlDocument reqData)
but reqData is always null. I have tried different types instead of XmlDocument.
Now if probably a good time to say that this is all new to me - I've not created a program to accept SOAP messages before.
Should I be creating a WCF Service Application (rather than a .Net Framework Class Library)?
I've tried adding the WSDL "definitions" but they don't seem to produce anything useful. E.g. there is reference to a "GetTransaction" method, but it has a parameter list of about 150 items!?
I have found quite a lot of stuff via Google but it always seems to be about sending, rather than receiving, SOAP messages.
I appreciate that I have not really included much code to look at, but I feel like I've started from the wrong place. So, any basic guidance, suggestions or links to tutorial sites would be most welcome.

How do I send a POST Request to Google Translate Api using a C# WebClient?

I googled now for almost an hour and can't find anything that would help me out. I'm a beginner programer and took the course over from TeamTreeHouse with Serialization in C#. Here I also learned how to use the WebClient.
Now I thought a good practice Project would be to make a Translate app that sends to google the user input in a POST Request and Google returns the answer as Json which I deserialize.
Problem is I read through the documentation of the api but I'm so confused of what I should send exactly to google and really how to do this?
I know the method webclient.Headers.Add(arguments here) , but I really don't know what else it needs.
You can find an example over here:
https://cloud.google.com/translate/docs/translating-text
https://translation.googleapis.com/language/translate/v2 Three query
parameters are required with each translation request:
Target language: Use the target parameter to specify the language you
want to translate into. Source text string: Use the q parameter to
specify each text string to translate. API key: Use the key parameter
to identify your application. If you are using OAuth 2.0 service
account credentials (recommended), do not supply this parameter.
So problem is there is no placeholder in that example URL where I could put my api key + soure text string an Target language.
So what exactly should I send to google so it knows what I want and returns me the JSON file?
Maybe anyone could help me out. And I know there is an official Library for exactly this but I want to practice serialization and using web scraping with the WebClient class so I want to do it like this.
You specifically asked about a POST.
You can use this url:
https://translation.googleapis.com/language/translate/v2?key=MY_KEY
Of course, replace MY_KEY with your key.
Add a header for the content type:
application/json; charset=utf-8
Format your text and target language as JSON and write it to your request stream:
{"q":"Team work is a major progress maker at this location, it appears that everyone is willing to help when they can.","target":"fr"}
The documentation here: https://cloud.google.com/translate/docs/reference/translate#body.QUERY_PARAMETERS
says use query parameters q, target, key, etc.
So your URL should be something like https://translation.googleapis.com/language/translate/v2?key=[yourAPIkey]&target=language

Https Communication to Java Server with Dotnet Client

I need to send financial messages(transactions) to a bank with the help of HTTPS communication. Bank server is written on Java while my application is in .Net.
Following are only details I received from their side:
Https request:
• The entire ISO request for https should be sent as an attachment to the following
servlet:
https:///Servlet/IBSOtherVendorRequestServlet
What are other components or documents required from their side and which namespace of .Net can I use for this communication?
Kindly help me on how to proceed.
You have to know how to query the servlet : SOAP ? REST ? what are the parameters and which method should be used ? result in plain text, XML or JSON ?
It looks like you have to POST a request to this Servlet. You have to know what is an "entire ISO request" then POST it to this url using HttpClient or HttpWebRequest. It will probably look like this : Upload files with HTTPWebrequest (multipart/form-data)

How can I communicate with the Kronos API?

I have a Kronos entry point http://kronos../wfc/XmlService that I should be able to access however when I open it in the brower the response is:
<Kronos_WFC>
<Response Status="Failure" ErrorCode="1332" Message="WFP-01110 The MIME type of the request is invalid. Type Found: . Valid types: text/xml, application/xml."></Response>
</Kronos_WFC>
What should I do to use the web services?
By the way, I'm using C# to communicate with the server.
You can communicate with the Kronos API using XML POST requests.
The WFC v5.0 Developer's Toolkit Programmer's Guide1 provides a general guide for communicating with the Kronos API in various languages. The first chapter covers the XML API, which is how all API requests will be sent.
Logon Request
A standard XML logon request for Kronos WFC. This must be sent first before any other requests.
<Kronos_WFC version="1.0">
<Request Object="System" Action="Logon" Username="ValidUsername" Password="ValidPassword"/>
</Kronos_WFC>
Response:
<Kronos_WFC version="1.0" TimeStamp="11/15/2017 3:35PM GMT-05:00">
<Response Status="Success" Timeout="1800" PersonKey="123456" Object="System" Username="ValidUsername" Action="Logon" PersonNumber="112233">
</Response>
</Kronos_WFC>
Logoff Request
This logoff request will end your active Kronos session.
<Kronos_WFC version="1.0">
<Request Object="System" Action="Logoff"/>
</Kronos_WFC>
Pay Period Total Request
This request loads the Pay Period Total for employee 12345 between October 20th 2017 and October 27th 2017.
<Kronos_WFC version="1.0">
<Request Action="Load">
<Timesheet>
<Employee>
<PersonIdentity PersonNumber="12345"/>
</Employee>
<Period>
<TimeFramePeriod PeriodDateSpan="10/20/2017 - 10/27/2017"/>
</Period>
</Timesheet>
</Request>
</Kronos_WFC>
A full list of Kronos API tags can be found in the Workforce Timekeeping Developer Toolkit Reference Guide (requires login).
Your method of sending POST requests may vary depending on your language. However, the XML request format and API entry point (<ServerName>/wfc/XmlService) should apply to all languages.
Below is an example Python 3 script for sending a Kronos logon request:
import requests
url = "http://localhost/wfc/XmlService"
headers = {'Content-Type': 'text/xml'}
data = """<Kronos_WFC version = "1.0">
<Request Object="System" Action="Logon" Username="SomeUsername" Password="SomePassword"/>
</Kronos_WFC>"""
# Login to Kronos and print response
session = requests.Session() # preserve login cookies across requests
response = session.post(url, data=data, headers=headers)
print(response.text)
Chapter 2 of the WFC Developer's Toolkit Programmer's Guide includes examples for sending XML requests in Java and Visual Basic. However, I recommend looking into a more up-to-date XML or HTTP requests library specific to whatever language you are using.
Helpful Resources
Workforce Timekeeper v8.0.16 Developer's Toolkit Reference Guide (requires login)
WFC v5.0 Developer's Toolkit Programmer's Guide
Documentation and Service Packs for Kronos Products (requires login)
WFC-API.js - JavaScript for sending XML requests to WFC API
TimeCardView - Open source web UI to the Kronos API
Kronos Community Forums
Footnotes:
1: The quoted documentation was originally written for Kronos WFC 5.0 (API 1.0). While later versions should use the same API, I cannot guarantee accuracy for other versions. (See XML API version 6.3 to 8.0 upgrade)
You get that with the brower because the Kronos server only support POST requests and the Browser is issuing a GET Request. The reason for that is because Kronos requires an XML in the body and the POST is the most adecuate method to do so.
The way to access the Kronos XML API, is making a WebRequest to the URL you have with the Method set to POST like this:
HttpWebRequest reqFp = (HttpWebRequest)HttpWebRequest.Create(KronosServerUrl);
reqFp.Method = "POST";
reqFp.ContentType = "text/xml";
Note how the ContentType is also set to text/xml.
Also,you need to make sure you have XML API access check in the Function Access Profile in Kronos.
Not only XML Access but the user also have to have remote api access enabled

Categories

Resources