YouTube Icon

Interview Questions.

Web API Interview Questions and Answers - Jul 15, 2022

fluid

Web API Interview Questions and Answers

Q1. What is Web API?

Ans: An application programming interface (API) is a fixed of subroutine definitions, information structures, item training, protocols, and gear for constructing software and packages.

To put it in simple word, API is a few sort of interface which has a set of features that permit programmers to get right of entry to precise capabilities or information of an application, running device or different offerings.

Web API because the call indicates, is an API over the web which may be accessed the use of HTTP protocol. It is a idea and not a generation. We can build Web API using specific technology along with PHP, Java, .NET and many others. For example, Twitter’s REST APIs provide programmatic get admission to to read and write information the usage of which we will combine twitter’s skills into our very own web utility.

Q2. What is REST?

Ans: REpresentational State Transfer. It describes how one device can speak kingdom with another. One example will be the state of a product (its call, description etc) represented as XML, JSON, or simple textual content. The generalised idea of state is termed a resource.

Q3. What are the Advantages of the usage of Web APIs

Ans: The benefits of the usage of an Application Programming Interface, or API, in Web development are based totally on an API’s potential to interact with Web pages.

1. External Database Access/Information sharing : APIs offer ability to website site visitors to get entry to far flung, password-included databases. Your API password and username authorizes permission for data or facts to be exchanged between your website and the APIs database-pushed sources. An API’s capabilities encompass connecting, fetching and remaining the get right of entry to to its affiliated server as important.

2. Convenience : Analyze the areas of your website’s offerings to discover features that an API can take care of. The right API can characteristic a lot like outsourcing inside the sense that you may use an API to manage elements of a web enterprise. Tasks which are useful but now not vital to the center of your enterprise can be addressed by way of an API; as an instance, you can use an API to address responsibilities to your behalf so you can attention at the most important features of your commercial enterprise.

Three. Security Protection : Some API applications get entry to additional servers as necessary. Protection from protection-related vulnerabilities is an API-particular advantage worth investigating as you pick out an API on your internet site. An API that received’t require you to expand consider to a couple of far flung servers can maintain your site’s protection. Examine the security risks associated with an API of interest before incorporating one into your website.

Four. Automation : The move platform programs are related with each other the use of the API and automate their technique conversation to keep away from manual interventions.

5. Extended potential to customize person enjoy : with an API an utility layer can be created which may be used to distribute statistics and services to new audiences which may be personalised to create custom consumer reviews.

6. Efficiency : whilst get admission to is supplied to an API, the content generated can be posted robotically and is available for each channel. It allows it to be shared and allotted extra effortlessly.

7. Integration :APIs permit content to be embedded from any web site or software extra effortlessly. This ensures greater fluid statistics shipping and an included consumer enjoy.

Q4. What are the alternatives to REST and the way does REST evaluate with them

Ans: This is akin to asking "How RESTful is my technique?" Use the subsequent list (summarised from the Richardson Maturity Model as described by way of Martin Fowler):

Level zero - The swamp of POX :Use POST for everything (reads, writes, deletes). This is SOAP, POX, RPI and many others. You're simply the use of HTTP as a tunnel to your personal protocol. You goal a unmarried endpoint that does the whole thing based totally at the contents of the request frame.

Level 1 - Resources : Use POST for the whole lot. Target a couple of endpoints designed to serve up records about a particular component. You've just determined assets.

Level 2 - HTTP verbs : Use HTTP verbs against sources. Now you're GETing it. POST is to create, PUT is to overwrite, OPTIONS for to be had operations, DELETE to, properly, delete the resource. As a end result of the use of these verbs distinctive HTTP repute codes begin to become greater relevant (202 ACCEPTED every body?).

Level 3 - Hypermedia manage (HATEOAS) : At this factor you make the final bounce and introduce hypermedia as a glide manage mechanism. A REST client wishes no prior know-how about how to engage with any precise application or server past a prevalent expertise of hypermedia. This can be communicated in HTTP thru the Content-Type header area. Text formats consist of AtomPub and (greater concise) HAL, whilst HyperAudio works properly for audio streams (see SoundCloud et al)

Q5. What is GET, POST, PUT and DELETE?

Ans: An HTTP request is a class inclusive of HTTP fashion requests, request lines, request techniques, request URL, header fields, and body content material. The maximum not unusual techniques which are used by a customer in an HTTP request are as follows:-

GET:- Used when the patron is asking for a resource at the Web server..

POST:- Used while the purchaser is sending data or data to the server—for instance, filling out a web shape (i.E. Sends a big quantity of complicated data to the Web Server).

PUT:- Used whilst the purchaser is sending a alternative document or importing a new report to the Web server underneath the request URL.

DELETE:- Used whilst the client is making an attempt to delete a record from the Web server, identified by using the request URL.

Q6. What position does it occupy in a web structure ecosystem?

Ans: REST is usually related to the web offerings interface due to the fact HTTP is through far the maximum common provider protocol. In the 7-layer model it exists on the utility layer. However, see the following segment.

Q7. What is distinction between PUT and POST?

Ans: POST is used to create a brand new entity. “Post” manner “after”; if you have a group of entities and you tack a brand new one onto its end, you have got posted to the gathering. You can’t publish an current entity, and it’s not unusual (although not usually required) to apply the collection’s URI to publish. (E.G. You would create a brand new Quora person named Noah through POSTing to https://quora.Com/users as opposed to https://quora.Com/users/noah.)

PUT is used to set an entity’s information completely. PUTting is similar to POSTing, besides that it'll overwrite the entity if already exists or create it otherwise. You ought to use a PUT to write a user in your database that could already be in it.

Q8. Explain Web API Routing?

Ans: Web API routing is much like ASP.NET MVC Routing. It routes an incoming HTTP request to a selected movement method on a Web API controller.

Web API helps  varieties of routing:

Convention-primarily based Routing

Attribute Routing

Q9. How tightly (or loosely) it's miles coupled with protocol?

Ans: REST is not HTTP. It uses HTTP due to the fact in its maximum standard shape REST exists to assist a system in mapping the idea of a verb towards an arbitrary collection of nouns. HTTP contains a useful set of generic verbs (GET, PUT, PATCH and so forth) which could applied to arbitrary nouns expresssed as URIs the usage of HTTP e.G. GET http://instance.Org/Product(fifty four).

Q10. What is HttpStatusCode? Any different ?

Ans: var statusCodes = new List<HttpStatusCode>()

HttpStatusCode.BadGateway,

HttpStatusCode.GatewayTimeout,

HttpStatusCode.HttpVersionNotSupported,

HttpStatusCode.InternalServerError,

HttpStatusCode.NotImplemented,

HttpStatusCode.ServiceUnavailable

;

if (statusCodes.Contains(reaction.StatusCode))

throw new HttpRequestException("Blah");

Q11. Can we create SOAP based totally message the usage of Web API?

Ans: We have a server which has numerous varieties of api (custom XML API primarily based on httplistener, SOAP API based on WCF and REST API based on WEB API). We need to move all API's to WEB API (there are numerous motives) and it need to be backward well matched.

One of the cause to support url shape: offerings/service1. Offerings/service2. And in this situation it ought to be on one port. It is intranet utility which is shipped to multiple customers and it must be easy to installation, install. So, we cannot have an extended configuration on client facet (proxing and otherts).

Q12. What are the Advantages of Using ASP.NET Web API?

Ans: If your project customers desires information in more than one formats (json,xml,csv) or have danger to alternate in future Wep Api wishes minimal configuration comparing to mvc. Wep Api returns records to consumer in keeping with content material negotiation (if purchaser wishes xml returns xml,if json go back json in line with request header ) however in mvc you need more code to meet that.You must explicitly specify information layout whilst writing action strategies.(JsonResult,ActionResult,XmlResult)

Wep Api offers you extra meaningful concept approximately what you're doing when you study the code later.Comparing method signatures; public List<Student> Get() has extra which means than public JsonResult Index().

Q13. Difference between Web API 1.Zero and Web API 2.Zero?

Ans: WebAPI and WebAPI 2

Actually WebAPI 2.0 is improved feature of WebApi there is no difference between this . In model 2.Zero, the Web API framework has been improved to assist the following capabilities:

IHttpActionResult go back type

A new Routing Attribute

Support for Cross-Origin requests using CORS

Securing ASP.NET Web API the use of OAuth 2.0

Support for $make bigger, $select in OData Service

Q14. Which Features are Introduced in ASP.NET Web API 2.Zero?

Ans: In its only form, a Web API is an API over the net (HTTP). ASP.NET Web API is a framework that lets in you to build Web API’s, i.E. HTTP-primarily based services on top of the .NET Framework the usage of a conference primarily based and comparable programming model, as that of ASP.NET MVC. These services can then be used in a wide range of customers, browsers and cellular gadgets.

Two variations of the ASP.NET Web API framework had been launched up to now, with Web API 2.0 being the modern day one. In this newsletter, we will explore some of the new features introduced in ASP.NET Web API 2.Zero.

Q15. Serialization and Deserialization in Web API?

Ans: public interface

IHttpMessageSerializer

void Serialize(HttpResponseMessage reaction, Stream stream);

void Serialize(HttpRequestMessage request, Stream movement);

HttpResponseMessage DeserializeToResponse(Stream move);

HttpRequestMessage DeserializeToRequest(Stream flow);

public class MessageContentHttpMessageSerializer : IHttpMessageSerializer

private bool _bufferContent;

public MessageContentHttpMessageSerializer() : this(fake)

public MessageContentHttpMessageSerializer(bool bufferContent)

_bufferContent = bufferContent;

public void Serialize(HttpResponseMessage reaction, Stream movement)

byte[] assuranceBuffer = null;

if (_bufferContent && response.Content != null)

assuranceBuffer = reaction.Content.ReadAsByteArrayAsync().Result; // make certain it's far buffered

var httpMessageContent = new HttpMessageContent(reaction);

var buffer = httpMessageContent.ReadAsByteArrayAsync().Result;

circulate.Write(buffer, zero, buffer.Length);

public void Serialize(HttpRequestMessage request, Stream circulate)

byte[] assuranceBuffer = null;

if (_bufferContent && request.Content != null)

assuranceBuffer = request.Content.ReadAsByteArrayAsync().Result; // make sure it's miles buffered

var httpMessageContent = new HttpMessageContent(request);

var buffer = httpMessageContent.ReadAsByteArrayAsync().Result;

movement.Write(buffer, zero, buffer.Length);

public HttpResponseMessage DeserializeToResponse(Stream move)

var reaction = new HttpResponseMessage();

var memoryStream = new MemoryStream();

circulation.CopyTo(memoryStream);

response.Content = new ByteArrayContent(memoryStream.ToArray());

response.Content.Headers.Add("Content-Type", "software/http;msgtype=reaction");

return reaction.Content.ReadAsHttpResponseMessageAsync().Result;

public HttpRequestMessage DeserializeToRequest(Stream circulation)

var request = new HttpRequestMessage();

var memoryStream = new MemoryStream();

stream.CopyTo(memoryStream);

request.Content = new ByteArrayContent(memoryStream.ToArray());

request.Content.Headers.Add("Content-Type", "software/http;msgtype=request");

return request.Content.ReadAsHttpRequestMessageAsync().Result;

Q16. Are there smooth manner for implementation SOAP carrier on web api?

Ans: At first look ought to be smooth way to parse httprequest to typed soap envelope (based on existed settlement) and serialize a solution. Of route, there many movements and facts kinds in contract. PS: I do not want to investigate servicestack:)

Update:The problem I described above may be fixed through proxing http request to soap carrier (It can work only with basichttpbinding without protection. If WCF carrier require NTLM authentication it won't paintings):

[HttpPost]  public async Task<

IHttpActionResult>

SoapAction() 

var httpClient = new HttpClient();

var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost:8111/soap")

Content = this.Request.Content

;

foreach (var header in this.Request.Headers)

httpRequestMessage.Headers.Add(header.Key, header.Value); 

var responseMessage= watch for httpClient.SendAsync(httpRequestMessage).ConfigureAwait(fake);

return ResponseMessage(responseMessage);

 

But I nonetheless need to understand are there any SOAP parser in C# due to the fact my server helps NTLM authentication.




CFG